Linux学习-第四周

一、统计出/etc/passwd文件中其默认shell为非/sbin/nologin的用户个数,并将用户都显示出来

(1) 统计个数
[root@CentOS8 ~]#grep "/sbin/nologin" /etc/passwd |wc -l
(2) 显示用户
法一: grep "/sbin/nologin" /etc/passwd |cut -d: -f1
法二:sed -En '/\/sbin\/nologin/s#^([^:]+):.$#\1#p' /etc/passwdshell

二、查出用户UID最大值的用户名、UID及shell类型

[root@CentOS8 ~]#grep ":cat /etc/passwd|cut -d: -f3|sort -nr|head -n1:" /etc/passwd |cut -d: -f1,3,7
nobody:65534:/sbin/nologinbash

三、统计当前链接本机的每一个远程主机IP的链接数,并按从大到小排序

ss -nt |tail -n+2|tr -s " " |cut -d" " -f5|cut -d: -f1 |uniq -c |sort -nr
或netstat -t |tail -n+3|tr -s " "|cut -d" " -f5 |cut -d: -f1|uniq -c |sort -nride

四、编写脚本disk.sh,显示当前硬盘分区中空间利用率最大的值

df |tail -n+2|sed -En 's#.[[:space:]]+([0-9]+)%.$#\1#p' |sort -nr|head -1
df |tail -n+2|tr -s " "|cut -d' ' -f5|cut -d% -f1|sort -nr|head -1
脚本disk.sh的内容
#!/bin/bash
df -h|tail -n+2|tr -s " "|cut -d' ' -f5|cut -d% -f1|sort -nr|head -1
#或 df |tail -n+2|sed -En 's#.[[:space:]]+([0-9]+)%.$#\1#p' |sort -nr|head -1
赋权限chmod +x disk.sh
执行脚本./disk.sh spa

五、编写脚本 systeminfo.sh,显示当前主机系统信息,包括:主机名,IPv4地址,操做系统版本,内核版本,CPU型号,内存大小,硬盘大小

[root@CentOS8 script]#cat systeminfo.sh
#!/bin/bash
RED="\E[1;31m"
GREEN="\E[1;32m"
END="\E[0m"
echo -e $GREEN----------------------Host systeminfo--------------------$END
echo -e "HOSTNAME: $REDhostname$END"
echo -e "IPADDR: $REDifconfig|grep -Eo '([0-9]{1,3}\.){3}[0-9]{1,3}'|head -n1$END"
echo -e "OSVERSION: $REDcat /etc/redhat-release$END"
echo -e "KERNEL: $REDuname -r$END"
echo -e "CPU: $REDlscpu|grep 'Model name'|tr -s ' '|cut -d : -f2$END"
echo -e "MEMORY: $REDfree -h|grep Mem|tr -s ' ' : |cut -d : -f2$END"
echo -e "DISK: $REDlsblk |grep '^sd' |tr -s ' ' |cut -d " " -f1,4 -z$END"
echo -e $GREEN---------------------------------------------------------$END操作系统

相关文章
相关标签/搜索