码迷,mamicode.com
首页 > 系统相关 > 详细

Linux基本功杂记——[004]——8月4日课后作业

时间:2016-08-05 15:20:45      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:

一、找出ip addr命令结果中本机所有的IPv4地址

BASH:

f@z ~/py $ ip addr | grep -oP (?:\d+\.){3}\d+/\d+
127.0.0.1/8
172.18.16.4/24

Python3:

f@z ~/py $ cat ipaddr.py 
#!/usr/bin/python3
#-*- coding=‘utf-8‘ -*-
import re
import subprocess
ipb = subprocess.check_output(ip addr, shell=True)
ipaddr = ipb.decode(utf-8)
ip1,ip2 = re.findall(r(?:\d+\.){3}\d+/\d+, ipaddr)
print(ip1, ip2)

f@z ~/py $ python3 ipaddr.py 
127.0.0.1/8 172.18.16.4/24

二、列出inode使用率最大的三个分区,列出分区名称、inode使用率

BASH:

f@z ~ $ df -i | sed -r s/ +/ /g | cut -d  -f1,5 | sort -rn -k2 | head -n3
/dev/root 3%
tmpfs 1%
tmpfs 1%

Python3:

f@z ~/py $ cat df.py 
#!/usr/bin/env python3
#-*- coding=utf-8 -*-
import re
import subprocess
import heapq
a = re.split(\n, subprocess.getoutput(df -i))
b = []
c = []
for x in a[1:]:
    z = re.split(\s+, x, 5)
    b.append(z[0])
    c.append(z[-2])
d = dict(zip(b,c))
print(heapq.nlargest(3, d.items(), key=lambda x: x[1]))

f@z ~/py $ python3 df.py 
[(/dev/root, 3%), (shm, 1%), (tmpfs, 1%)]

三、查找UID最大的用户名、UID、SHELL

BASH:

f@z ~/py $ sort -nr -t : -k 3 passwd | head -n 1 | cut -d : -f 1,3,7
nobody:65534:/bin/false

Python3:

f@z ~/py $ cat pw.py 
#!/usr/bin/env python3
#_*_ coding:UTF-8 _*_
import re
with open(/home/f/py/passwd, r) as f:
    a = f.readlines()
    b = []
    for x in a:
        b.append(re.split(:, x))
    c = max(b, key=lambda s: int(s[2]))
    name,_,uid,*_,shell = c
    print(name, uid, shell.strip())

f@z ~/py $ python3 pw.py 
nobody 65534 /bin/false

四、查出/tmp的权限并以数字方式显示

BASH:

f@z ~/py $ stat /tmp/ | sed -n 4p | grep -oP (?<=\()\d{3,4}(?=/)
1777

Python3:

f@z ~/py $ cat tmpperm.py 
#!/usr/bin/env python3
#-*- coding=utf-8 -*-
import re
import subprocess
a = re.split(\n, subprocess.getoutput(stat /tmp))[3]
print(re.findall((?<=\()\d{3,4}(?=/), a))

f@z ~/py $ python3 tmpperm.py 
[1777]

五、统计当前本机连接的远程主机IP数量,并按从大到小排列

Python3&BASH:

z py # cat ss.py && python3 ss.py && ss -tun | grep -oP ‘(?:\d+\.){3}\d+(?=:\d+\s*$)‘ | sort | uniq -c | sort -nr

#!/usr/bin/env python3
#-*- coding=‘utf-8‘ -*-
import re
import subprocess
a = re.findall(r(?:\d+\.){3}\d+(?=:\d+\s*), subprocess.getoutput(ss -tun))[1::2]
b = set(a)
c = []
for x in b:
    c.append(a.count(x))
d = dict(zip(b,c))
print(sorted(d.items(), key=lambda z: z[1], reverse=True))

Python3输出:
[(
74.125.23.102, 3), (42.121.254.229, 2), (74.125.23.100, 1), (220.181.105.153, 1)]

BASH输出:
3 74.125.23.102 2 42.121.254.229 1 74.125.23.100 1 220.181.105.153

Linux基本功杂记——[004]——8月4日课后作业

标签:

原文地址:http://www.cnblogs.com/hadex/p/5738540.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!