1智什、統(tǒng)計出/etc/passwd文件中其默認shell為非/sbin/nologin的用戶個數(shù)篙骡,并將用戶都顯示出來
grep -v '/sbin/nologin$' /etc/passwd | grep -oE "^\w+" | tee file3.txt | wc -l >> file3.txt
[root@cenos8 ~]# grep -v '/sbin/nologin$' /etc/passwd | grep -oE "^\w+" | tee file3.txt | wc -l >> file3.txt
[root@cenos8 ~]# cat file3.txt
root
sync
shutdown
halt
mxx
user1
user2
user3
8
2庵楷、查出用戶UID最大值的用戶名、UID及shell類型
sort -t: -k3 -nr /etc/passwd | head -n 1 | grep -Eo '^\w+|[[:digit:]]+|\W\w+\W\w+$' | uniq
[root@cenos8 ~]# sort -t: -k3 -nr /etc/passwd | head -n 1 | grep -Eo '^\w+|[[:digit:]]+|\W\w+\W\w+$' | uniq
nobody
65534
/sbin/nologin
3橄抹、統(tǒng)計當(dāng)前連接本機的每個遠程主機IP的連接數(shù)内斯,并按從大到小排序
ss -ant | tail -n +2 | tr -s ' ' % |cut -d% -f 5| grep -oE "(([0-9]{1,3}\.){3}[0-9]{0,3})|\[.*\]" | sort -n -r| uniq -c
或
ss -ant | grep -Eo '((([0-9]{1,3}\.){3}[0-9]{0,3})|\[::\]):(\*|[[:digit:]]+)[[:space:]]*$' | grep -oE "(([0-9]{1,3}\.){3}[0-9]{0,3})|\[.*\]" | sort -n -r | uniq -c
[root@cenos8 ~]# ss -ant | tail -n +2 | tr -s ' ' % |cut -d% -f 5| grep -oE "(([0-9]{1,3}\.){3}[0-9]{0,3})|\[.*\]" | sort -n -r| uniq -c
1 192.168.1.7
5 0.0.0.0
4 [::]
[root@cenos8 ~]# ss -ant
State Recv-Q Send-Q Local Address:Port Peer Address:PortProcess
LISTEN 0 128 127.0.0.1:6010 0.0.0.0:*
LISTEN 0 128 0.0.0.0:111 0.0.0.0:*
LISTEN 0 32 192.168.122.1:53 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 5 127.0.0.1:631 0.0.0.0:*
ESTAB 0 36 192.168.1.200:22 192.168.1.7:1028
LISTEN 0 128 [::1]:6010 [::]:*
LISTEN 0 128 [::]:111 [::]:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 5 [::1]:631 [::]:*
[root@cenos8 ~]#
4、編寫腳本disk.sh净薛,顯示當(dāng)前硬盤分區(qū)中空間利用率最大的值
[root@cenos8 ~]# cat > disk.sh <<EOF
> #汪榔!/bin/bash
> echo -e "\e[1;32mHightest Hard disk utilization: \e[0m"
> df -lh | grep -Eo "[[:digit:]]{1,2}%"| sort -rn | head -1
> EOF
[root@cenos8 ~]# chmod +x disk.sh
[root@cenos8 ~]# ./disk.sh
Hightest Hard disk utilization:
21%
5、編寫腳本 systeminfo.sh肃拜,顯示當(dāng)前主機系統(tǒng)信息痴腌,包括:主機名,IPv4地址燃领,操作系統(tǒng)版本士聪,內(nèi)核版本,CPU型號猛蔽,內(nèi)存大小剥悟,硬盤大小
#!/bin/bash
#*********************************************************
RED="\e[1;31m"
GREEN="\e[1;32m"
SKYBLUE="\e[1;36m"
YELLOW="\e[1;43m"
BLUE="\e[1;44m"
END="\e[0m"
#*********************Host Systeminfo*********************
echo -e "HOSTNAME: $SKYBLUE`hostname`$END"
echo -e "IPv4_Address: $SKYBLUE`ifconfig ens33 | grep -i mask|grep -o '\([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}' | head -n 1`$END"
echo -e "OS_VERSION: $SKYBLUE`lsb_release -a | grep Description | tr -d [[:cntrl:]] | cut -d: -f 2`$END"
echo -e "LINUX_CORE_VERSION: $SKYBLUE`uname -r`$END"
echo -e "CPU_MODEL: $SKYBLUE`lscpu | grep "^Model name" | tr -s ' '| cut -d : -f 2`$END"
echo -e "MEMERY_SIZE: $SKYBLUE`free -h | tr -s ' ' | cut -d' ' -f 2 | head -n 2 | tail -n 1`$END"
echo -e "DISK_SIZE: $SKYBLUE`lsblk | tail -n +2 | head -n 1 | grep -Eo '[[:digit:]]+G'`$END"
[root@cenos8 ~]# ./systeminfo.sh
HOSTNAME: cenos8.mxx.com
IPv4_Address: 192.168.1.200
OS_VERSION: CentOS Linux release 8.4.2105
LINUX_CORE_VERSION: 4.18.0-305.3.1.el8.x86_64
CPU_MODEL: AMD Ryzen 7 4800H with Radeon Graphics
MEMERY_SIZE: 1.9Gi
DISK_SIZE: 200G
[root@cenos8 ~]#