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操作系統

相關文章
相關標籤/搜索