1、统计出/etc/passwd文件中其默认shell为非/sbin/nologin的用户个数,并将用户都显示出来
[root@centos7 ~]# echo "用户个数为":`cat /etc/passwd | grep -v '/sbin/nologin' | wc -l` && echo "用户为:" `cat /etc/passwd | grep -v '/sbin/nologin' | cut -d':' -f1 `
用户个数为:11
用户为: root sync shutdown halt guo mage distro mageia user1 user2 user3
[root@centos7 ~]#
2、查出用户UID最大值的用户名、UID及shell类型
方法1
[root@localhost ~]# cat /etc/passwd | sort -t: -k3 -nr | head -1 | cut -d: -f1,3,7
user3:2005:/bin/bash
[root@centos7 ~]#
方法2
[root@centos7 ~]# cut -d : -f1,3,7 /etc/passwd | sort -t: -k2 -nr | head -1
user3:2005:/bin/bash
[root@centos7 ~]#
3、统计当前连接本机的每个远程主机IP的连接数,并按从大到小排序
方法1
[root@centos7 ~]# ss -nt | tail -n +2 | tr -s ' ' : | cut -d: -f6 | sort |uniq -c
3 192.168.234.1
1 192.168.234.100
[root@centos7 ~]#
方法2
[root@centos7 ~]# ss -nt | awk '{print $5}' | grep -oE '([0-9]{3}\.){3}[0-9]{1,3}' | sort -n | uniq -c
3 192.168.234.1
1 192.168.234.100
[root@centos7 ~]#
4、编写脚本disk.sh,显示当前硬盘分区中空间利用率最大的值
[root@centos7 ~]# cat disk.sh
#!/bin/bash
df -h | tail -n +2 |tr -s ' ' % | cut -d% -f5 | sort -nr | head -1
[root@centos7 ~]#
[root@centos7 ~]# sh disk.sh
19
[root@centos7 ~]#
5、编写脚本 systeminfo.sh,显示当前主机系统信息,包括:主机名,IPv4地址,操作系统版本,内核版本,CPU型号,内存大小,硬盘大小
[root@centos7 ~]# cat systeminfo.sh
#!/bin/bash
GREEN="\e[1;32m"
RED="\e[1;35m"
BLUE="\e[1;36m"
END="\e[0m"
echo -e $GREEN-----------------------主机信息-----------------------$END
echo -e "$BLUE"主机名:"$END $RED`hostname`"$END
echo -e "$BLUE"IP地址:"$END $RED`ifconfig ens33 | grep inet | tr -s ' ' % |cut -d% -f3|head -1`"$END
echo -e "$BLUE"操作系统:"$END $RED`cat /etc/redhat-release`"$END
echo -e "$BLUE"内核版本:"$END $RED`uname -r`"$END
echo -e "$BLUE"CPU型号:"$END $RED`lscpu | grep 'Model name' |tr -s ' '| cut -d: -f2`"$END
echo -e "$BLUE"内存大小:"$END $RED`free -h | grep 'Mem'|tr -s ' ' : | cut -d: -f2`"$END
echo -e "$BLUE"硬盘大小:"$END $RED`lsblk | grep '^sd' | tr -s ' ' % | cut -d% -f4`"$END
echo -e $GREEN------------------------------------------------------$END
[root@centos7 ~]#
执行结果