grep cut 及显示系统硬件信息命令

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

[root@centos8 ~]# cat /etc/passwd |egrep -v '/sbin/nologin'  //统计
root:x:0:0:root:/root:/bin/bash
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
wang:x:1000:1000:wang:/home/wang:/bin/bash
[root@centos8 ~]# cat /etc/passwd |egrep -v '/sbin/nologin'|wc -l  //显示行数
5
[root@centos8 ~]#  cat /etc/passwd |egrep -v '/sbin/nologin' |cut -d: -f1  //cut剪切 显示用户
root
sync
shutdown
halt
wang

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

cat /etc/passwd | sort -nr -t ":"  -k 3|head -1|cut -d ":" -f 1,3,7 ||cat /etc/passwd | sort -n -t ":"  -k 3|tail -1|cut -d ":" -f 1,3,7//查看pwaawd文件,并用sor命令以“:”作为分隔符对第三列进行倒序排序,用head命令取出第一列,再用cut命令以“:”作为分隔符取第1、3、7|| 
nobody:65534:/sbin/nologin

3、统计当前主机远程ip的连接数,并排序

[root@centos8 ~]# ss -nt|grep  "^ESTAB" |tr -s ' ' :       //统计出连接数,并替换空格为“:” 方便使用cut
ESTAB:0:0:10.0.0.201:22:10.0.0.1:51409:
ESTAB:0:0:10.0.0.201:22:10.0.0.1:51410:
ESTAB:0:0:10.0.0.201:22:10.0.0.1:51406:
ESTAB:0:0:10.0.0.201:22:10.0.0.1:51395:
ESTAB:0:0:10.0.0.201:22:10.0.0.1:51391:
ESTAB:0:0:10.0.0.201:22:10.0.0.1:50424:
ESTAB:0:48:10.0.0.201:22:10.0.0.1:50406:
ESTAB:0:0:10.0.0.201:22:10.0.0.1:51394:
[root@centos8 ~]# ss -nt|grep  "^ESTAB" |tr -s ' ' : |cut -d: -f6|uniq -c|sort -rn|head -1  //排序 并取第一名
      1 51410

4、编写脚本显示利用率最大的值

vim disk.sh
#/bin/bash   //

df|sed "1d"|tr -s " " ":"|cut -d ":" -f5|sort -nr|head -1    //df删除第一行之后tr “:”替换“ ” 在使用cut裁剪以“:”作为分隔符的第六列进行倒序排序取第一。
运行结果如下:
[root@centos8 data]# . disk.sh
6%

5、编写脚本,显示主机信息。

vim systeminfo.sh

#/bin/bash   //
HOSTNAME=`hostname`
CPUID=`lscpu |egrep "Model name"|cut -d ":" -f2|tr -s " "`
IP=`ifconfig ens33|grep "inet "|tr -s " " ":"|cut -d ":" -f3`
CENTOS=`uname -a |cut -d " " -f2`
FREE=`free -h |grep "Mem"|tr -s " "|cut -d " " -f2`
KERNEL=`uname -a |cut -d " " -f3|cut -d "-" -f1`
DISK=`fdisk -l |grep   "Disk /dev/" |cut -d " " -f3|paste -sd +|bc`
echo " 主机名:                $HOSTNAME"
echo " CPU型号:              $CPUID"
echo " IP地址:                $IP"
echo " Centos版本:            $CENTOS"
echo " 内核版本:              $KERNEL"
echo " 内存大小:              $FREE"
echo " 硬盘总大小:            $DISK Gb"

[root@centos8 data]# . systeminfo.sh
 主机名:                centos8.0
 CPU型号:               Intel(R) Core(TM) i5-10400 CPU @ 2.90GHz
 IP地址:                10.0.0.201
 Centos版本:            centos8.0
 内核版本:              4.18.0
 内存大小:              1.7Gi
 硬盘总大小:            240 Gb


猜你喜欢

转载自blog.51cto.com/15132577/2677837