grep实例和shell脚本
2020-12-20 本文已影响0人
沐熙一叶_Leaf
1、统计出/etc/passwd文件中其默认shell为非/sbin/nologin的用户个数,并将用户都显示出来
[root@localhost ~]# grep -v '/sbin/nologin' /etc/passwd|cut -d: -f1
root
sync
shutdown
halt
2、查出用户UID最大值的用户名、UID及shell类型
[root@localhost ~]# getent passwd | sort -t : -k 3 -nr | head -n1 | cut -d: -f1,3,7
nobody:65534:/sbin/nologin
[root@localhost ~]# getent passwd | sort -t : -k 3 -nr | cut -d: -f1,3,7 | head -n1
nobody:65534:/sbin/nologin
3、统计当前连接本机的每个远程主机IP的连接数,并按从大到小排序
[root@localhost ~]# ss -tun |grep ESTAB | tr -s " " ":" | cut -d: -f7 | sort | uniq -c | sort -nr
2 10.0.0.1
[root@localhost ~]# ss -tun |grep ESTAB | tr -s " " | cut -d" " -f6 | cut -d: -f1 | sort | uniq -c | sort -nr
2 10.0.0.1
4、编写脚本disk.sh,显示当前硬盘分区中空间利用率最大的值
[root@localhost ~]# vim disk.sh
#!/bin/bash
df -h | grep -e /dev/sd -e /dev/nvme | tr -s " " | cut -d" " -f5 | sort -nr | head -n3
[root@localhost ~]# chmod +x disk.sh
[root@localhost ~]# ./disk.sh
16%
5、编写脚本 systeminfo.sh,显示当前主机系统信息,包括:主机名,IPv4地址,操作系统版本,内核版本,CPU型号,内存大小,硬盘大小
[root@localhost ~]# vim systeminfo.sh
#!/bin/bash
RED="\E[1;31m"
GREEN="\E[1;32m"
END="\E[0m"
echo -e "$GREEN----------------------Host systeminfo--------------------$END"
echo -e "HOSTNAME: $RED`hostname`$END"
echo -e "IPADDR: $RED` ifconfig ens160|grep -Eo '([0-9]{1,3}\.){3}[0-9]
{1,3}' |head -n1`$END"
echo -e "OSVERSION: $RED`cat /etc/redhat-release`$END"
echo -e "KERNEL: $RED`uname -r`$END"
echo -e "CPU: $RED`lscpu|grep 'Model name'|tr -s ' '|cut -d : -f2`$END"
echo -e "MEMORY: $RED`free -h|grep Mem|tr -s ' ' : |cut -d : -f2`$END"
echo -e "DISK: $RED`lsblk |grep '^nv' |tr -s ' ' |cut -d " " -f4`$END"
echo -e "$GREEN---------------------------------------------------------$END"
[root@localhost ~]# chmod +x systeminfo.sh
[root@localhost ~]# ./systeminfo.sh
----------------------Host systeminfo--------------------
HOSTNAME: localhost.localdomain
IPADDR: 10.0.0.2
OSVERSION: CentOS Linux release 8.2.2004 (Core)
KERNEL: 4.18.0-193.el8.x86_64
CPU: Intel(R) Core(TM) i7-10710U CPU @ 1.10GHz
MEMORY: 791Mi
DISK: 40G
---------------------------------------------------------