练习1

1.找出ifconfig"网卡名"命令结果中本机的IPv4地址
1.1CentOS6

[root@centos6 ~]# ifconfig eth0 |head -n2 | tail -n1 | tr -s " " |cut -d" " -f3 |cut -d: -f2
192.168.43.132
[root@centos6 ~]# ifconfig lo |head -n2 | tail -n1 | tr -s " " |cut -d" " -f3 |cut -d: -f2
127.0.0.1

1.2CentOS7

[root@centos7 ~]# ifconfig ens32 | head -n2 | tail -n1 |tr -s " "|cut -d" " -f3
192.168.43.133
[root@centos7 ~]# ifconfig lo | head -n2 | tail -n1 |tr -s " "|cut -d" " -f3
127.0.0.1

2.查出分区空间使用率的最大百分比值
2.1方法一:

[root@centos6 ~]# df
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/sda2      100660656 4558020  90982636   5% /
tmpfs             501508      72    501436   1% /dev/shm
/dev/sda1         999320   34124    912768   4% /boot
/dev/sda3       50264772   53064  47651708   1% /data
[root@centos6 ~]# df | tr -s " "|cut -d" " -f5|cut -d% -f1|tr -dc [0-9'\n']|sort -nr|head -n1
5

2.2方法二:

[root@centos6 ~]# df
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/sda2      100660656 4558020  90982636   5% /
tmpfs             501508      72    501436   1% /dev/shm
/dev/sda1         999320   34124    912768   4% /boot
/dev/sda3       50264772   53064  47651708   1% /data
[root@centos6 ~]# df |tr -s " " %|cut -d% -f5|sort -n|tail -1
5

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

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

4.查出/tmp的权限,以数字方式显示
4.1方法一:

[root@centos7 ~]# stat /tmp
  File: ‘/tmp’
  Size: 4096        Blocks: 8          IO Block: 4096   directory
Device: 802h/2050d  Inode: 134320200   Links: 29
Access: (1777/drwxrwxrwt)  Uid: (    0/    root)   Gid: (    0/    root)
Context: system_u:object_r:tmp_t:s0
Access: 2020-02-23 01:49:24.793376858 +0800
Modify: 2020-02-22 22:20:33.607411416 +0800
Change: 2020-02-22 22:20:33.607411416 +0800
 Birth: -
[root@centos7 ~]# stat /tmp |head -n4 |tail -n1 |cut -d'(' -f2|cut -d'/' -f1
1777

4.2方法二:

[root@centos7 ~]# stat /tmp 
  File: ‘/tmp/’
  Size: 4096        Blocks: 8          IO Block: 4096   directory
Device: 802h/2050d  Inode: 134320200   Links: 29
Access: (1777/drwxrwxrwt)  Uid: (    0/    root)   Gid: (    0/    root)
Context: system_u:object_r:tmp_t:s0
Access: 2020-02-23 01:49:24.793376858 +0800
Modify: 2020-02-22 22:20:33.607411416 +0800
Change: 2020-02-22 22:20:33.607411416 +0800
 Birth: -
[root@centos7 ~]# stat /tmp/ | tr -cs '[:digit:]' ":"|cut -d: -f9
1777

4.3方法三:

[root@centos7 ~]# stat /tmp 
  File: ‘/tmp/’
  Size: 4096        Blocks: 8          IO Block: 4096   directory
Device: 802h/2050d  Inode: 134320200   Links: 29
Access: (1777/drwxrwxrwt)  Uid: (    0/    root)   Gid: (    0/    root)
Context: system_u:object_r:tmp_t:s0
Access: 2020-02-23 01:49:24.793376858 +0800
Modify: 2020-02-22 22:20:33.607411416 +0800
Change: 2020-02-22 22:20:33.607411416 +0800
 Birth: -
[root@centos7 ~]# stat -c %a /tmp/
1777

5.统计当前连接本机的每个远程主机IP的连接数,并按从大到小排序
5.1方法一:

[root@centos6 ~]# netstat -nt
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State      
tcp        0      0 192.168.43.132:22           192.168.43.1:43122          ESTABLISHED 
tcp        0      0 192.168.43.132:22           192.168.43.133:53314        ESTABLISHED 
tcp        0      0 192.168.43.132:22           192.168.43.133:53316        ESTABLISHED 
[root@centos6 ~]# netstat -nt|tail -n3|tr -s " " :|cut -d: -f6|sort |uniq -c|sort -r
  2 192.168.43.133
  1 192.168.43.1
注:此方法有明显不足,当连接数量较多,或者需要处理,分析文件较大时,tail -n3这个命令明显不适用。所以,此方法只是自己做实验时梳理思路是用,实际生产中还是需要利用grep过滤或者正则表达式进行匹配会有更好的效果。

5.2方法二:

[root@centos6 ~]# netstat -t
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State      
tcp        0      0 192.168.43.132:ssh          192.168.43.1:43122          ESTABLISHED 
tcp        0      0 192.168.43.132:ssh          192.168.43.133:53314        ESTABLISHED 
tcp        0      0 192.168.43.132:ssh          192.168.43.133:53316        ESTABLISHED 
[root@centos6 ~]# netstat -t | grep ':ssh'|tr -s ' '|cut -d ' ' -f5|cut -d: -f1 | uniq -c|sort -rn
      2 192.168.43.133
      1 192.168.43.1

你可能感兴趣的:(练习1)