系统信息查看:

top、df -h、w、cat /proc/net/dev、、、


psutil--很方便地获取系统信息

首先安装psutil:

pip3 install psutil

然后进入Python3:

>>> import psutil
>>> psutil.cpu_count()
4
>>> psutil.cpu_count(logical=False)
2
>>> psutil.cpu_times()
scputimes(user=16759.24, nice=0.0, system=12043.98, idle=189336.42)
>>> psutil.cpu_percent()
18.1
>>> psutil.cpu_percent(percpu=True)
[25.6, 11.9, 22.8, 11.2]
>>> psutil.cpu_percent(percpu=True)
[7.1, 2.8, 5.6, 1.6]
>>> psutil.cpu_percent(percpu=True)
[13.0, 5.8, 11.5, 5.5]
>>> psutil.cpu_percent(percpu=True)
[27.4, 16.5, 24.3, 15.1]
>>> psutil.cpu_percent(percpu=True)
[7.7, 2.9, 5.8, 1.9]
>>> psutil.cpu_percent(percpu=True,interval=5)
[7.6, 2.4, 5.4, 2.0]
>>> psutil.virtual_memory()
svmem(total=8589934592, available=1706909696, percent=80.1, used=6201737216, free=442769408, active=3082399744, inactive=1264140288, wired=1855197184)
>>> psutil.swap_memory()
sswap(total=1073741824, used=317980672, free=755761152, percent=29.6, sin=31907336192, sout=443731968)
>>> psutil.disk_partitions()
[sdiskpart(device='/dev/disk1s1', mountpoint='/', fstype='apfs', opts='rw,local,rootfs,dovolfs,journaled,multilabel'), sdiskpart(device='/dev/disk1s4', mountpoint='/private/var/vm', fstype='apfs', opts='rw,noexec,local,dovolfs,dontbrowse,journaled,multilabel,noatime'), sdiskpart(device='/dev/disk2s1', mountpoint='/Volumes/Mac资料存储', fstype='hfs', opts='rw,nosuid,local,dovolfs,ignore-ownership,multilabel')]
>>> psutil.disk_usage('/')
sdiskusage(total=250790436864, used=179777757184, free=67658543104, percent=72.7)
>>> psutil.disk_io_counters
<function disk_io_counters at 0x104a4c510>
>>> psutil.disk_io_counters()
sdiskio(read_count=2191034, write_count=1301421, read_bytes=32851836928, write_bytes=21891323904, read_time=379654, write_time=332387)
>>> psutil.net_io_counters()
snetio(bytes_sent=642163712, bytes_recv=5593271296, packets_sent=3858155, packets_recv=4482255, errin=0, errout=0, dropin=0, dropout=0)
>>> psutil.net_io_counters(pernic=True)
{'lo0': snetio(bytes_sent=106320896, bytes_recv=106320896, packets_sent=152692, packets_recv=152692, errin=0, errout=0, dropin=0, dropout=0), 'gif0': snetio(bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0), 'stf0': snetio(bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0), 'XHC20': snetio(bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0), 'en0': snetio(bytes_sent=535782400, bytes_recv=5484661760, packets_sent=3704893, packets_recv=4327902, errin=0, errout=0, dropin=0, dropout=0), 'p2p0': snetio(bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0), 'awdl0': snetio(bytes_sent=297984, bytes_recv=2491392, packets_sent=1230, packets_recv=1950, errin=0, errout=0, dropin=0, dropout=0), 'en1': snetio(bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0), 'en2': snetio(bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0), 'bridge0': snetio(bytes_sent=0, bytes_recv=0, packets_sent=1, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0), 'utun0': snetio(bytes_sent=0, bytes_recv=0, packets_sent=2, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0), 'vnic0': snetio(bytes_sent=0, bytes_recv=61440, packets_sent=0, packets_recv=281, errin=0, errout=0, dropin=0, dropout=0), 'vnic1': snetio(bytes_sent=0, bytes_recv=1024, packets_sent=0, packets_recv=11, errin=0, errout=0, dropin=0, dropout=0)}
>>> 

#进程相关信息
>>> psutil.pids()
[7877, 7876, 7874, 7872, 7870, 7869, 7868, 7841, 7700, 7691, 7656, 7655, 7652, 7644, 7581, 7538, 7532, 7434, 7433, 7432, 7431, 7424, 7416, 7394, 7392, 7391, 7374, 7369, 7368, 7367, 7365, 7363, 7333, 7332, 7329, 7328, 7327, 7326, 7325, 7321, 7318, 7316, 7310, 7309, 7302, 7301, 7297, 7295, 7294, 7250, 7214, 6998, 6855, 6850, 6811, 6810, 6809, 6789, 6786, 6710, 6670, 6385, 6382, 6119, 5203, 5149, 5097, 4983, 4980, 4967, 3946, 3937, 3863, 3852, 3506, 3086, 3069, 2848, 2795, 2537, 2191, 1966, 1964, 1826, 1783, 1580, 1097, 1092, 993, 849, 848, 757, 737, 733, 712, 693, 691, 690, 635, 632, 627, 626, 617, 609, 606, 603, 599, 598, 596, 589, 586, 575, 574, 552, 541, 537, 535, 533, 532, 530, 529, 527, 522, 521, 519, 509, 507, 506, 500, 498, 497, 495, 494, 491, 489, 484, 482, 480, 479, 478, 477, 474, 473, 472, 471, 467, 466, 465, 464, 461, 457, 446, 441, 439, 437, 434, 433, 430, 428, 426, 425, 424, 422, 419, 417, 405, 403, 402, 400, 399, 398, 396, 395, 394, 393, 391, 385, 384, 382, 379, 378, 376, 373, 371, 369, 366, 365, 363, 361, 355, 354, 353, 351, 350, 349, 348, 345, 338, 336, 335, 334, 332, 331, 329, 328, 325, 324, 323, 321, 320, 318, 317, 314, 313, 311, 310, 309, 308, 306, 305, 304, 303, 302, 301, 296, 295, 294, 293, 289, 287, 286, 285, 282, 281, 278, 276, 275, 274, 273, 272, 271, 270, 268, 266, 265, 264, 262, 260, 259, 258, 246, 220, 218, 216, 215, 214, 210, 208, 201, 200, 176, 169, 168, 167, 163, 162, 161, 160, 158, 157, 154, 152, 150, 149, 122, 120, 119, 118, 116, 113, 109, 107, 106, 104, 103, 102, 101, 100, 99, 98, 97, 95, 94, 93, 92, 91, 88, 87, 86, 84, 82, 81, 80, 79, 78, 77, 76, 75, 72, 71, 68, 66, 65, 64, 59, 57, 53, 52, 50, 49, 45, 44, 43, 40, 39, 1, 0, 6780, 5844, 5038, 4612, 2562, 2482, 1824, 1742, 1730, 1403, 751]
>>> p = psutil.Process(100)
>>> p
psutil.Process(pid=100, name='AirPlayXPCHelper', started='2018-07-11 00:23:11')
>>> p.status
<bound method Process.status of psutil.Process(pid=100, name='AirPlayXPCHelper', started='2018-07-11 00:23:11')>
>>> p.status()
'running'
>>> p.exe()
'/usr/libexec/AirPlayXPCHelper'
>>> p.cwd()
>>> p.creat_time()
>>> p.uids()
puids(real=0, effective=0, saved=0)
>>> p.gids()
puids(real=0, effective=0, saved=0)
>>> p.cpu_times()
>>> p.memory_percent()
>>> p.io_counters()
>>> p.num_threads()
>>> p.connections()
>>> 

>>> psutil.boot_time()
1531239808.0
>>> import datetime
>>> datetime.datetime.fromtimestamp(1531239808).strftime("%Y-%m-%d %H:%M:%S")
'2018-07-11 00:23:28'
>>> psutil.users()
[suser(name='tiramisu', terminal='console', host=None, started=1531239808.0, pid=92), suser(name='tiramisu', terminal='ttys000', host=None, started=1531500032.0, pid=7432)]
>>> 


IPy--高效地处理IP地址相关的信息

首先安装IPy:

pip3 install psutil

进入Python3环境;

>>> import IPy
>>> IPy.IP("159.65.130.172").version()
4
>>> IPy.IP("::1").version()
6
>>> 
>>> ips = IPy.IP('192.168.1.0/28')
>>> ips.len()
16
>>> [ip for ip in ips]
[IP('192.168.1.0'), IP('192.168.1.1'), IP('192.168.1.2'), IP('192.168.1.3'), IP('192.168.1.4'), IP('192.168.1.5'), IP('192.168.1.6'), IP('192.168.1.7'), IP('192.168.1.8'), IP('192.168.1.9'), IP('192.168.1.10'), IP('192.168.1.11'), IP('192.168.1.12'), IP('192.168.1.13'), IP('192.168.1.14'), IP('192.168.1.15')]
>>>  
... 
>>> ip = IPy.IP('192.168.88.1')
>>> ip.iptype()
'PRIVATE'
>>> ip = IPy.IP('8.8.8.8')
>>> ip.iptype()
'PUBLIC'
>>> ip.int()
134744072
>>> ip.strHex()
'0x8080808'
>>> ip.strBin()
'00001000000010000000100000001000'
>>> 
>>> 
>>> IPy.IP('192.168.1.0').make_net('255.255.255.0')
IP('192.168.1.0/24')
>>> IPy.IP('192.168.1.0/255.255.255.248',make_net=True)
IP('192.168.1.0/29')
>>> IPy.IP('192.168.1.0-192.168.1.255',make_net=True)
IP('192.168.1.0/24')
>>> 
>>> 
>>> '192.168.4.3' in IPy.IP('192.168.1.0/24')
False
>>> '192.168.4.3' in IPy.IP('192.168.4.0/24')
True
>>> 
>>> 
>>> IPy.IP('192.168.0.0/23').overlaps('192.168.1.0')
1
>>> IPy.IP('192.168.0.0/23').overlaps('192.168.1.0/24')
1
>>> IPy.IP('192.168.0.0/24').overlaps('192.168.1.0/24')
0
>>> 


dnspython--实现对dns的一些监控以及解析结果的校验

首先安装好dnspython:

pip3 install dnspython

然后进入Python3运行:

>>> import dns.resolver
>>> res = dns.resolver.query("www.baidu.com","A")
>>> res
<dns.resolver.Answer object at 0x103563630>
>>> [item.address for item in res]
['14.215.177.38', '14.215.177.39']
>>> 
>>> res = dns.resolver.query("qq.com","MX")
>>> [item for item in res]
[<DNS IN MX rdata: 30 mx1.qq.com.>, <DNS IN MX rdata: 20 mx2.qq.com.>, <DNS IN MX rdata: 10 mx3.qq.com.>]
>>> 
>>> res = dns.resolver.query("runtofuture.cn","NS")
>>> [item for item in res]
[<DNS IN NS rdata: f1g1ns2.dnspod.net.>, <DNS IN NS rdata: f1g1ns1.dnspod.net.>]
>>> 


Last modification:July 14th, 2018 at 07:40 pm
If you think my article is useful to you, please feel free to appreciate