继续学习

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

答:cut -d: -f1,7 /etc/passwd | grep -v '/sbin/nologin' | wc -l:

① 以冒号为分隔符,抽取/etc/passwd文件的第1、7列的内容。

② 通过管道技术,把cut命令的内容标准输入重定向到grep命令,然后查看不匹配/sbin/nologin的行。

③ 通过管道技术,把grep命令的内容标准输入重定向到wc命令,然后只统计行总数。

如图所示,在/etc/passwd文件中,默认shell类型为非/sbin/nologin的用户有16个。

[root@centos8 ~]#cut -d: -f1,7 /etc/passwd | grep -v '/sbin/nologin' | wc -l
16
[root@centos8 ~]#

cut -d: -f1,7 /etc/passwd | grep -v '/sbin/nologin' | cut -d: -f1:

① 以冒号为分隔符,抽取/etc/passwd文件的第1、7列的内容。

② 通过管道技术,把cut命令的内容标准输入重定向到grep命令,然后查看不匹配/sbin/nologin的行。

③ 通过管道技术,把grep命令的内容标准输入重定向到cut命令,然后以冒号为分隔符,抽取第1列的内容。

[root@centos8 ~]#cut -d: -f1,7 /etc/passwd | grep -v '/sbin/nologin' | cut -d: -f1
root
sync
shutdown
halt
qian
test
qc
user
gentoo
tomcat
git
mysql
mageia
user1
user2
user3
[root@centos8 ~]#

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

答:cut -d: -f1,3,7 /etc/passwd | sort -t: -k2 -nr | head -n1

① 以冒号为分隔符,抽取/etc/passwd文件的第1、3、7列的内容。

② 通过管道技术,把cut命令的内容标准输入重定向到sort命令,然后按照使用冒号分隔的第2列,再按照数字(UID)从大到小顺序(倒序)排列。

③ 通过管道技术,把sort命令的内容标准输入重定向到head命令,然后查看第1行内容。

[root@centos8 ~]#cut -d: -f1,3,7 /etc/passwd | sort -t: -k2 -nr | head -n1
nobody:65534:/sbin/nologin
[root@centos8 ~]#

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

答:ss -nt | tail -n +2 | tr -s ' ' ':' | cut -d: -f6 | sort | uniq -c | sort -nr

① 查看目前有哪些主机(含端口)正在连接本服务器。

② 通过管道技术,把ss命令的内容标准输入重定向到tail命令,然后查看第2行到最后一行的内容。这样,可以查看ss命令的去掉首行,显示其余行的内容。

③ 通过管道技术,把tail命令的内容标准输入重定向到tr命令,然后相邻重复的空格符只保留一个(去重),再把空格符替换成冒号。

④ 通过管道技术,把tr命令的内容标准输入重定向到cut命令,然后以冒号为分隔符,抽取第6列的内容。

⑤ 通过管道技术,把cut命令的内容标准输入重定向到sort命令,然后按照默认顺序排列。

⑥ 通过管道技术,把sort命令的内容标准输入重定向到uniq命令,然后显示每行重复出现的次数。

⑦ 通过管道技术,把uniq命令的内容标准输入重定向到sort命令,然后按照数字(每行重复出现的次数)从大到小顺序(倒序)排列。

[root@centos8 ~]#ss -nt
State   Recv-Q   Send-Q     Local Address:Port     Peer Address:Port    
ESTAB   0        0             10.0.0.201:22         10.0.0.202:43242   
ESTAB   0        52            10.0.0.201:22           10.0.0.1:50201   
ESTAB   0        0             10.0.0.201:22         10.0.0.203:49918   
ESTAB   0        0             10.0.0.201:22         10.0.0.202:43240   
ESTAB   0        0             10.0.0.201:22           10.0.0.1:55162   
ESTAB   0        0             10.0.0.201:22           10.0.0.1:55166   
[root@centos8 ~]#ss -nt | tail -n +2 | tr -s ' ' ':' | cut -d: -f6 | sort | uniq -c | sort -nr
      3 10.0.0.1
      2 10.0.0.202
      1 10.0.0.203
[root@centos8 ~]#

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

答:方法一:

[root@centos8 data]#vim /data/disk.sh
[root@centos8 data]#cat /data/disk.sh
#!/bin/bash
#
#************************************************************
#Author:        qc
#QQ             285076228
#Date:          2021-06-29
#FileName       disk.sh
#URL:
#Description:
#Copyright (C): 2021 All rights reserved
#************************************************************

df | tail -n +2 | tr -s ' ' % | cut -d% -f5 | sort -nr | head -n1
[root@centos8 data]#
[root@centos8 data]#bash /data/disk.sh
21
[root@centos8 data]#bash ./disk.sh
21
[root@centos8 data]#

方法二:

[root@centos8 data]#vim /data/disk3.sh
[root@centos8 data]#cat /data/disk3.sh
#!/bin/bash
#
#************************************************************
#Author:        qc
#QQ             285076228
#Date:          2021-07-13
#FileName       /data/disk3.sh
#URL:
#Description:
#Copyright (C): 2021 All rights reserved
#************************************************************

df | grep '/dev/sd' | tr -s ' ' % | cut -d% -f5 | sort -nr | head -n1
[root@centos8 data]#
[root@centos8 data]#ll /data/disk3.sh
-rw-r--r-- 1 root root 373 Jul 13 23:06 /data/disk3.sh
[root@centos8 data]#chmod a+x /data/disk3.sh
[root@centos8 data]#ll /data/disk3.sh
-rwxr-xr-x 1 root root 373 Jul 13 23:06 /data/disk3.sh
[root@centos8 data]#
[root@centos8 data]#/data/disk3.sh
21
[root@centos8 data]#./disk3.sh
21
[root@centos8 data]#

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

答:

[root@centos8 data]#vim /data/systeminfo.sh 
[root@centos8 data]#cat /data/systeminfo.sh
#!/bin/bash
#
#************************************************************
#Author:        qc
#QQ             285076228
#Date:          2021-06-29
#FileName       /data/systeminfo.sh
#URL:
#Description:
#Copyright (C): 2021 All rights reserved
#************************************************************
echo "查看当前主机系统信息:"
echo "1、主机名:       `hostname`"
echo "2、IPv4地址:     `hostname -I | cut -d' ' -f1`"
echo "3、操作系统版本: `cat /etc/redhat-release`"
echo "4、内核版本:     `uname -r`"
echo "5、CPU型号:     `lscpu | grep '^Model name' | tr -s ' ' | cut -d: -f2`"
echo "6、内存大小:     `free -h | grep '^Mem' | tr -s ' ' | cut -d' ' -f2`"
echo "7、硬盘大小:     `lsblk | grep '^sda' | tr -s ' ' | cut -d' ' -f4`"
[root@centos8 data]#
[root@centos8 data]#bash /data/systeminfo.sh
查看当前主机系统信息:
1、主机名:       centos8
2、IPv4地址:     10.0.0.201
3、操作系统版本: CentOS Linux release 8.2.2004 (Core) 
4、内核版本:     4.18.0-193.el8.x86_64
5、CPU型号:      Intel(R) Core(TM) i5-7500 CPU @ 3.40GHz
6、内存大小:     1.8Gi
7、硬盘大小:     200G
[root@centos8 data]#
posted @ 2021-06-29 21:35  qcjiayou  阅读(154)  评论(1)    收藏  举报