1、统计出/etc/passwd文件中其默认shell为非/sbin/nologin的用户个数,并将用户都显示出来
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、统计当前连接本机的每个远程主机IP的连接数,并按从大到小排序
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,显示当前硬盘分区中空间利用率最大的值
[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,显示当前主机系统信息,包括:主机名,IPv4地址,操作系统版本,内核版本,CPU型号,内存大小,硬盘大小
#!/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 ~]#