automationOperationsWithPython
1.psutil
系统性能信息模块,可获取系统运行的进程和系统利用率(包括CPU、内存、磁盘、网络等)信息。它主要应用于系统监控,分析和限制系统资源及进程的管理。该模块需要单独安装。
示例代码
1 import psutil 2 mem = psutil.virtual_memory() 3 print('mem'.center(40,'-')) 4 print(mem.total/1024/1024/1024,mem.used/1024/1024/1024) 5 print('cpu'.center(40,'-')) 6 print(psutil.cpu_times()) 7 print(psutil.cpu_count()) #CPU的逻辑核数 8 print(psutil.cpu_count(logical=False)) #CPU的物理核数 9 print('disk'.center(40,'-')) 10 print(psutil.disk_partitions()) 11 print(psutil.disk_usage('C:\\')) 12 print('network'.center(40,'-')) 13 print(psutil.net_io_counters(pernic=True)) 14 print('system info'.center(40,'-')) 15 print(psutil.users()) 16 print(psutil.boot_time()) 17 print('Process management'.center(40,'-')) 18 19 20 结果: 21 ------------------mem------------------- 22 11.956729888916016 5.612152099609375 23 ------------------cpu------------------- 24 scputimes(user=186734.71875, system=100156.75, idle=1456536.125, interrupt=2525.906265258789, dpc=1431.5468788146973) 25 4 26 4 27 ------------------disk------------------ 28 [sdiskpart(device='C:\\', mountpoint='C:\\', fstype='NTFS', opts='rw,fixed'), sdiskpart(device='D:\\', mountpoint='D:\\', fstype='NTFS', opts='rw,fixed'), sdiskpart(device='E:\\', mountpoint='E:\\', fstype='NTFS', opts='rw,fixed')] 29 sdiskusage(total=104752738304, used=33958334464, free=70794403840, percent=32.4) 30 ----------------network----------------- 31 {'isatap.{CB331A52-3599-4BA2-97A1-31EF9B483E7F}': snetio(bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0), 'isatap.{2605EAFF-8D67-4EC3-9772-572C4E85C758}': snetio(bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0), 'isatap.{DB84AAF2-A034-48DC-BC7E-E584E6A44BC3}': snetio(bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0), '本地连接* 2': snetio(bytes_sent=996962, bytes_recv=11480, packets_sent=7330, packets_recv=77, errin=0, errout=3, dropin=0, dropout=0), 'VMware Network Adapter VMnet1': snetio(bytes_sent=47636, bytes_recv=967, packets_sent=963, packets_recv=967, errin=0, errout=0, dropin=0, dropout=0), '本地连接': snetio(bytes_sent=3739710026, bytes_recv=17102011212, packets_sent=11978414, packets_recv=17316928, errin=0, errout=0, dropin=0, dropout=0), 'VMware Network Adapter VMnet8': snetio(bytes_sent=53035, bytes_recv=43032, packets_sent=26003, packets_recv=41235, errin=0, errout=0, dropin=0, dropout=0), 'Loopback Pseudo-Interface 1': snetio(bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0)} 32 --------------system info--------------- 33 [suser(name='Administrator', terminal=None, host='0.0.0.0', started=1471455110.0)] 34 1471455022.0 35 -----------Process management-----------
2.业务服务监控
2.1 字符串或文件对比
1 import difflib,sys 2 3 text1 = ''' 4 [pthread(id=5234, user_time=22.5, system_time=9.2891), 5 pthread(id=5235, user_time=0.0, system_time=0.0), 6 pthread(id=5236, user_time=0.0, system_time=0.0), 7 pthread(id=5237, user_time=0.0707, system_time=1.1)] 8 ''' 9 ntext1 = text1.splitlines() 10 11 text2 = """ 12 [pthread(id=5234, user_system_time=9.2891), 13 pthread(id=5235, u system_time=0.0), 14 pthread(id=5236, user_timystem_time=0.0), 15 pthread(id=5237, user_time=0.0707, system_time=1.1)] 16 """ 17 ntext2 = text2.splitlines() 18 print(type(ntext2)) 19 20 d = difflib.HtmlDiff() 21 #diff = d.compare(ntext1, ntext2) 22 print(d.make_file(ntext1,ntext2))
2.2 字符串或文件对比,生成HTML
1 import difflib,sys 2 3 try: 4 textfile1 = 'C:/Users/Administrator/workspace0725/day04/Atm/test/1.txt' 5 textfile2 = 'C:/Users/Administrator/workspace0725/day04/Atm/test/2.txt' 6 except Exception as e: 7 print('ERROR:%s' % str(e)) 8 print('USAGE:diffile.py filename1 filename2') 9 sys.exit() 10 11 def readFile(filename): 12 try: 13 f = open(filename,'r') 14 fr = f.read().splitlines() 15 f.close 16 return fr 17 except IOError as error: 18 print('Read file error:%s' % error) 19 sys.exit() 20 21 if textfile1 == '' or textfile2 == '': 22 print('USAGE:.PY FILENAME1 FILENAME2') 23 sys.exit() 24 25 26 ntext1 = readFile(textfile1) 27 ntext2 = readFile(textfile2) 28 29 d = difflib.HtmlDiff() 30 print(d.make_file(ntext1,ntext2))
2.3 单文件、多文件、目录对比
filecmp.cmp,可指定shallow参数来决定是否判断文件内容。
filecmp.cmpfiles
filecmp.dircmp。
2.4 递归比较目录
源目录里面有的或者是更新过的文件、目录,会强制同步到目标目录里,源里没有而目标里有的,保持原状
1 import filecmp,re,os,sys,shutil 2 holderlist = [] 3 4 def compareFile(dir1,dir2): 5 dircomp = filecmp.dircmp(dir1,dir2) 6 onlyInOne = dircomp.left_only 7 diffInOne = dircomp.diff_files 8 9 [holderlist.append(os.path.abspath(os.path.join(dir1,x))) for x in onlyInOne] 10 [holderlist.append(os.path.abspath(os.path.join(dir1,x))) for x in diffInOne] 11 if len(dircomp.common_dirs) > 0: 12 for item in dircomp.common_dirs: 13 compareFile(os.path.join(dir1,item), os.path.join(dir2,item)) 14 return holderlist 15 16 def main(): 17 if len(sys.argv) > 2: 18 dir1 = sys.argv[1] 19 dir2 = sys.argv[2] 20 else: 21 print('请输入两个目录。') 22 sys.exit() 23 sourceList = compareFile(dir1, dir2) 24 dir1 = os.path.abspath(dir1) 25 if not dir2.endswith('/'):dir2 = dir2 + '/' 26 dir2 = os.path.abspath(dir2) 27 desFiles = [] 28 createFlag = False 29 30 for item in sourceList: 31 desDir = re.sub(dir1, dir2, item) 32 if os.path.isdir(item): 33 if not os.path.exists(desDir): 34 os.makedirs(desDir) 35 createFlag = True 36 37 if createFlag: 38 sourceList = [] 39 desFiles = [] 40 sourceList = compareFile(dir1, dir2) 41 for item in sourceList: 42 desDir = re.sub(dir1, dir2, item) 43 desFiles.append(desDir) 44 45 print('update item:') 46 print(sourceList) 47 copyPair = zip(sourceList,desFiles) 48 for item in copyPair: 49 if os.path.isfile(item[0]): 50 if os.path.exists(item[0]): 51 shutil.copyfile(item[0], item[1]) 52 if __name__ == '__main__': 53 main()
2.5 使用smtplib模块发送电子邮件
本例仅介绍如何使用smtplib发送文本内容邮件,关于发送html、mime、表格等的操作请自行查阅。
1 #coding:utf-8 2 3 import smtplib 4 import email.mime.multipart 5 import email.mime.text 6 7 msg=email.mime.multipart.MIMEMultipart() 8 msg['from']='发送方邮箱' 9 msg['to']='收件邮箱' 10 msg['subject']='test' 11 content=''''' 12 你好, 13 这是一封测试邮件,请忽略。 14 15 www.sfbest.com 16 ''' 17 txt=email.mime.text.MIMEText(content) 18 msg.attach(txt) 19 20 smtp=smtplib 21 smtp=smtplib.SMTP() 22 smtp.connect('smtp.XXXXX.com','25') 23 smtp.login('发送方邮箱账号','发送方邮箱密码') 24 smtp.sendmail('发送方邮箱','接收方邮箱',str(msg)) 25 smtp.quit()
注意,新注册的邮箱好像不行,邮箱过滤会自动屏蔽消息,最好用自己常用的邮箱测试。
2.6 使用pycurl探测web服务质量
代码如下:
#!/usr/bin/evn python
#coding:utf-8
import os,sys,time,pycurl
URL = "http://www.baidu.com"
c = pycurl.Curl() #创建一个Curl对象
3.系统安全
3.1使用python-nmap实现高效的端口扫描
官网示例代码
1 >>> import nmap 2 >>> nm = nmap.PortScanner() 3 >>> nm.scan('127.0.0.1', '22-443') 4 >>> nm.command_line() 5 'nmap -oX - -p 22-443 -sV 127.0.0.1' 6 >>> nm.scaninfo() 7 {'tcp': {'services': '22-443', 'method': 'connect'}} 8 >>> nm.all_hosts() 9 ['127.0.0.1'] 10 >>> nm['127.0.0.1'].hostname() 11 'localhost' 12 >>> nm['127.0.0.1'].state() 13 'up' 14 >>> nm['127.0.0.1'].all_protocols() 15 ['tcp'] 16 >>> nm['127.0.0.1']['tcp'].keys() 17 [80, 25, 443, 22, 111] 18 >>> nm['127.0.0.1'].has_tcp(22) 19 True 20 >>> nm['127.0.0.1'].has_tcp(23) 21 False 22 >>> nm['127.0.0.1']['tcp'][22] 23 {'state': 'open', 'reason': 'syn-ack', 'name': 'ssh'} 24 >>> nm['127.0.0.1'].tcp(22) 25 {'state': 'open', 'reason': 'syn-ack', 'name': 'ssh'} 26 >>> nm['127.0.0.1']['tcp'][22]['state'] 27 'open' 28 29 >>> for host in nm.all_hosts(): 30 >>> print('----------------------------------------------------') 31 >>> print('Host : %s (%s)' % (host, nm[host].hostname())) 32 >>> print('State : %s' % nm[host].state()) 33 >>> for proto in nm[host].all_protocols(): 34 >>> print('----------') 35 >>> print('Protocol : %s' % proto) 36 >>> 37 >>> lport = nm[host][proto].keys() 38 >>> lport.sort() 39 >>> for port in lport: 40 >>> print ('port : %s\tstate : %s' % (port, nm[host][proto][port]['state'])) 41 ---------------------------------------------------- 42 Host : 127.0.0.1 (localhost) 43 State : up 44 ---------- 45 Protocol : tcp 46 port : 22 state : open 47 port : 25 state : open 48 port : 80 state : open 49 port : 111 state : open 50 port : 443 state : open 51 52 53 >>> print(nm.csv()) 54 host;protocol;port;name;state;product;extrainfo;reason;version;conf 55 127.0.0.1;tcp;22;ssh;open;OpenSSH;protocol 2.0;syn-ack;5.9p1 Debian 5ubuntu1;10 56 127.0.0.1;tcp;25;smtp;open;Exim smtpd;;syn-ack;4.76;10 57 127.0.0.1;tcp;53;domain;open;dnsmasq;;syn-ack;2.59;10 58 127.0.0.1;tcp;80;http;open;Apache httpd;(Ubuntu);syn-ack;2.2.22;10 59 127.0.0.1;tcp;111;rpcbind;open;;;syn-ack;;10 60 127.0.0.1;tcp;139;netbios-ssn;open;Samba smbd;workgroup: WORKGROUP;syn-ack;3.X;10 61 127.0.0.1;tcp;443;;open;;;syn-ack;; 62 63 64 >>> nm.scan(hosts='192.168.1.0/24', arguments='-n -sP -PE -PA21,23,80,3389') 65 >>> hosts_list = [(x, nm[x]['status']['state']) for x in nm.all_hosts()] 66 >>> for host, status in hosts_list: 67 >>> print('{0}:{1}'.host) 68 192.168.1.0:down 69 192.168.1.1:up 70 192.168.1.10:down 71 192.168.1.100:down 72 192.168.1.101:down 73 192.168.1.102:down 74 192.168.1.103:down 75 192.168.1.104:down 76 192.168.1.105:down 77 [...] 78 79 80 81 >>> nma = nmap.PortScannerAsync() 82 >>> def callback_result(host, scan_result): 83 >>> print '------------------' 84 >>> print host, scan_result 85 >>> 86 >>> nma.scan(hosts='192.168.1.0/30', arguments='-sP', callback=callback_result) 87 >>> while nma.still_scanning(): 88 >>> print("Waiting >>>") 89 >>> nma.wait(2) # you can do whatever you want but I choose to wait after the end of the scan 90 >>> 91 192.168.1.1 {'nmap': {'scanstats': {'uphosts': '1', 'timestr': 'Mon Jun 7 11:31:11 2010', 'downhosts': '0', 'totalhosts': '1', 'elapsed': '0.43'}, 'scaninfo': {}, 'command_line': 'nmap -oX - -sP 192.168.1.1'}, 'scan': {'192.168.1.1': {'status': {'state': 'up', 'reason': 'arp-response'}, 'hostname': 'neufbox'}}} 92 ------------------ 93 192.168.1.2 {'nmap': {'scanstats': {'uphosts': '0', 'timestr': 'Mon Jun 7 11:31:11 2010', 'downhosts': '1', 'totalhosts': '1', 'elapsed': '0.29'}, 'scaninfo': {}, 'command_line': 'nmap -oX - -sP 192.168.1.2'}, 'scan': {'192.168.1.2': {'status': {'state': 'down', 'reason': 'no-response'}, 'hostname': ''}}} 94 ------------------ 95 192.168.1.3 {'nmap': {'scanstats': {'uphosts': '0', 'timestr': 'Mon Jun 7 11:31:11 2010', 'downhosts': '1', 'totalhosts': '1', 'elapsed': '0.29'}, 'scaninfo': {}, 'command_line': 'nmap -oX - -sP 192.168.1.3'}, 'scan': {'192.168.1.3': {'status': {'state': 'down', 'reason': 'no-response'}, 'hostname': ''}}} 96 97 >>> nm = nmap.PortScannerYield() 98 >>> for progressive_result in nm.scan('127.0.0.1/24', '22-25'): 99 >>> print(progressive\_result)
实测代码
没有深入研究,测试着主机的状态有点不理解,比如有些主机ping是不可达的,但是用nmap.state()返回状态是up,不懂原理何在。
1 #!/usr/bin/env python 2 #coding:utf-8 3 4 5 import sys,nmap 6 7 scan_row = [] 8 userInput = input('>') 9 scan_row = userInput.split(' ') 10 11 if len(scan_row) < 2: 12 print('shuru host and port') 13 sys.exit() 14 15 hosts = scan_row[0] 16 ports = scan_row[1] 17 18 try: 19 nm = nmap.PortScanner() 20 except nmap.PortScannerError: 21 print("Nmap not found",sys.exc_info()) 22 sys.exit() 23 except: 24 print("Unexcepcted error:",sys.exc_info()) 25 sys.exit() 26 27 try: 28 nm.scan(hosts, arguments=' -v -sS -p '+ports) 29 except : 30 print("Scan error") 31 32 for host in nm.all_hosts(): 33 print('start'.center(40,'-')) 34 print('HOST: %s (%s)' % (host,nm[host].hostname())) 35 print('State: %s' % nm[host].state()) 36 for proto in nm[host].all_protocols(): 37 print('proto'.center(40,'-')) 38 print('Protocaol: %s' % proto) 39 40 lport = nm[host][proto].keys() 41 print(lport) 42 # lport.sort() 43 for port in lport: 44 print('port: %s\tstate:%s' % (port,nm[host][proto][port]['state']))