练习
1、统计出/etc/passwd文件中其默认shell为非/sbin/nologin的用户个数,并将用户都显示出来
[root@localhost date]# grep -vE '/sbin/nologin$' /etc/passwd | cut -d: -f1
2、查出用户UID最大值的用户名、UID及shell类型
[root@localhost date]# sort -t : -k 3 -nr /etc/passwd | head -1|cut -d : -f 1,3,7
3、统计当前连接本机的每个远程主机IP的连接数,并按从大到小排序
[root@localhost date]# netstat -t | grep 'ssh\>'|tr -s ":" " " |cut -d " " -f6 |uniq -c |sort -nr
4、编写脚本disk.sh,显示当前硬盘分区中空间利用率最大的值
[root@localhost date]# ./disk.sh
17%
[root@localhost date]# cat disk.sh
#!/bin/bash
echo `df | grep -E '/dev/sd'|tr -s " " |cut -d " " -f5|sort -nr | head -1`
5、编写脚本 systeminfo.sh,显示当前主机系统信息,包括:主机名,IPv4地址,操作系统版本,内核版本,CPU型号,内存大小,硬盘大小
[root@localhost date]# ./systeminfo.sh
-------------------------SYSTEMINFO----------------------
hostname is localhost.localdomain
ip add is 192.168.1.44
os is CentOS Linux release 8.4.2105
kernel 4.18.0-305.3.1.el8.x86_64
cpu Intel(R) Core(TM) i5-10210U CPU @ 1.60GHz
memory 1.7Gi
diskinfo 200G
---------------------------------------------------------
[root@localhost date]# cat systeminfo.sh
#!/bin/bash
green="\E[1;32m"
red="echo -e \E[1;31m"
end="\E[0m"
$red-------------------------SYSTEMINFO----------------------$end
echo -e "hostname is $green `hostname`$end"
echo -e "ip add is $green `ifconfig eth0 |sed -nr '2s#.*inet (([0-9]+\.){3}[0-9]+).*#\1#p'`$end"
echo -e "os is $green `lsb_release -a |grep "Description"|cut -d : -f2`$end"
echo -e "kernel $green `uname -r`$end"
echo -e "cpu $green `lscpu |grep 'Model name'| tr -s " "| cut -d : -f2 |head -1`$end"
echo -e "memory $green `free -h | grep 'Mem'|tr -s ":" " " | cut -d " " -f2`$end"
echo -e "diskinfo $green `lsblk | grep 'disk'|tr -s " "|cut -d " " -f 4`$end"
$red---------------------------------------------------------$end
[root@localhost date]#