shell脚本

  1. 统计出/etc/passwd文件中其默认shell为非/sbin/nologin的用户个数,并将用户都显示出来
    • awk -F: 'BEGIN{count=0}{if($NF!="/sbin/nologin"){print $1; count++}}END{printf "default shell is not nologin has %d user\n", count}' /etc/passwd
  2. 查出用户UID最大值的用户名、UID及shell类型
    • sort -t: -k3 -n /etc/passwd | tail -1 | awk -F: '{print $1, $3, $NF}'
  3. 统计当前连接本机的每个远程主机IP的连接数,并按从大到小排序
    • ss -tn4 | awk -F "[: ]+" 'NR>1 {print $(NF-2)}' | sort | uniq -c | sort -n
  4. 编写脚本disk.sh,显示当前硬盘分区中空间利用率最大的值
~]# cat disk.sh
df -lh | awk -F"[ %]+" 'BEGIN{MAX_USE="0"; COL=5}NR>1{if($COL>MAX_USE){MAX_USE=$COL}}END{print MAX_USE}'
  1. 编写脚本 systeminfo.sh,显示当前主机系统信息,包括:主机名,IPv4地址,操作系统版本,内核版本,CPU型号,内存大小,硬盘大小
~]# cat systeminfo.sh
hostname
hostname -I
cat /etc/centos-release
uname -r
lscpu | grep "Model name"
free -h | awk 'NR>1{print $1,$2}'
lsblk | grep "^[a-z]" |awk '{print $4}'