automationOperationsWithPython

automationOperationsWithPython

1.psutil

系统性能信息模块,可获取系统运行的进程和系统利用率(包括CPU、内存、磁盘、网络等)信息。它主要应用于系统监控,分析和限制系统资源及进程的管理。该模块需要单独安装。

示例代码

 import psutil
mem = psutil.virtual_memory()
print('mem'.center(40,'-'))
print(mem.total/1024/1024/1024,mem.used/1024/1024/1024)
print('cpu'.center(40,'-'))
print(psutil.cpu_times())
print(psutil.cpu_count()) #CPU的逻辑核数
print(psutil.cpu_count(logical=False)) #CPU的物理核数
print('disk'.center(40,'-'))
print(psutil.disk_partitions())
print(psutil.disk_usage('C:\\'))
print('network'.center(40,'-'))
print(psutil.net_io_counters(pernic=True))
print('system info'.center(40,'-'))
print(psutil.users())
print(psutil.boot_time())
print('Process management'.center(40,'-')) 结果:
------------------mem-------------------
11.956729888916016 5.612152099609375
------------------cpu-------------------
scputimes(user=186734.71875, system=100156.75, idle=1456536.125, interrupt=2525.906265258789, dpc=1431.5468788146973)
4
4
------------------disk------------------
[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')]
sdiskusage(total=104752738304, used=33958334464, free=70794403840, percent=32.4)
----------------network-----------------
{'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)}
--------------system info---------------
[suser(name='Administrator', terminal=None, host='0.0.0.0', started=1471455110.0)]
1471455022.0
-----------Process management-----------

2.业务服务监控

2.1 字符串或文件对比

 import difflib,sys

 text1 = '''
[pthread(id=5234, user_time=22.5, system_time=9.2891),
pthread(id=5235, user_time=0.0, system_time=0.0),
pthread(id=5236, user_time=0.0, system_time=0.0),
pthread(id=5237, user_time=0.0707, system_time=1.1)]
'''
ntext1 = text1.splitlines() text2 = """
[pthread(id=5234, user_system_time=9.2891),
pthread(id=5235, u system_time=0.0),
pthread(id=5236, user_timystem_time=0.0),
pthread(id=5237, user_time=0.0707, system_time=1.1)]
"""
ntext2 = text2.splitlines()
print(type(ntext2)) d = difflib.HtmlDiff()
#diff = d.compare(ntext1, ntext2)
print(d.make_file(ntext1,ntext2))

2.2 字符串或文件对比,生成HTML

 import difflib,sys

 try:
textfile1 = 'C:/Users/Administrator/workspace0725/day04/Atm/test/1.txt'
textfile2 = 'C:/Users/Administrator/workspace0725/day04/Atm/test/2.txt'
except Exception as e:
print('ERROR:%s' % str(e))
print('USAGE:diffile.py filename1 filename2')
sys.exit() def readFile(filename):
try:
f = open(filename,'r')
fr = f.read().splitlines()
f.close
return fr
except IOError as error:
print('Read file error:%s' % error)
sys.exit() if textfile1 == '' or textfile2 == '':
print('USAGE:.PY FILENAME1 FILENAME2')
sys.exit() ntext1 = readFile(textfile1)
ntext2 = readFile(textfile2) d = difflib.HtmlDiff()
print(d.make_file(ntext1,ntext2))

2.3 单文件、多文件、目录对比

filecmp.cmp,可指定shallow参数来决定是否判断文件内容。

filecmp.cmpfiles

filecmp.dircmp。

2.4 递归比较目录

源目录里面有的或者是更新过的文件、目录,会强制同步到目标目录里,源里没有而目标里有的,保持原状

 import filecmp,re,os,sys,shutil
holderlist = [] def compareFile(dir1,dir2):
dircomp = filecmp.dircmp(dir1,dir2)
onlyInOne = dircomp.left_only
diffInOne = dircomp.diff_files [holderlist.append(os.path.abspath(os.path.join(dir1,x))) for x in onlyInOne]
[holderlist.append(os.path.abspath(os.path.join(dir1,x))) for x in diffInOne]
if len(dircomp.common_dirs) > 0:
for item in dircomp.common_dirs:
compareFile(os.path.join(dir1,item), os.path.join(dir2,item))
return holderlist def main():
if len(sys.argv) > 2:
dir1 = sys.argv[1]
dir2 = sys.argv[2]
else:
print('请输入两个目录。')
sys.exit()
sourceList = compareFile(dir1, dir2)
dir1 = os.path.abspath(dir1)
if not dir2.endswith('/'):dir2 = dir2 + '/'
dir2 = os.path.abspath(dir2)
desFiles = []
createFlag = False for item in sourceList:
desDir = re.sub(dir1, dir2, item)
if os.path.isdir(item):
if not os.path.exists(desDir):
os.makedirs(desDir)
createFlag = True if createFlag:
sourceList = []
desFiles = []
sourceList = compareFile(dir1, dir2)
for item in sourceList:
desDir = re.sub(dir1, dir2, item)
desFiles.append(desDir) print('update item:')
print(sourceList)
copyPair = zip(sourceList,desFiles)
for item in copyPair:
if os.path.isfile(item[0]):
if os.path.exists(item[0]):
shutil.copyfile(item[0], item[1])
if __name__ == '__main__':
main()

2.5 使用smtplib模块发送电子邮件

本例仅介绍如何使用smtplib发送文本内容邮件,关于发送html、mime、表格等的操作请自行查阅。

 #coding:utf-8

 import smtplib
import email.mime.multipart
import email.mime.text msg=email.mime.multipart.MIMEMultipart()
msg['from']='发送方邮箱'
msg['to']='收件邮箱'
msg['subject']='test'
content='''''
你好,
这是一封测试邮件,请忽略。 www.sfbest.com
'''
txt=email.mime.text.MIMEText(content)
msg.attach(txt) smtp=smtplib
smtp=smtplib.SMTP()
smtp.connect('smtp.XXXXX.com','')
smtp.login('发送方邮箱账号','发送方邮箱密码')
smtp.sendmail('发送方邮箱','接收方邮箱',str(msg))
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对象

automationOperationsWithPython-LMLPHP

3.系统安全

3.1使用python-nmap实现高效的端口扫描

官网示例代码

 >>> import nmap
>>> nm = nmap.PortScanner()
>>> nm.scan('127.0.0.1', '22-443')
>>> nm.command_line()
'nmap -oX - -p 22-443 -sV 127.0.0.1'
>>> nm.scaninfo()
{'tcp': {'services': '22-443', 'method': 'connect'}}
>>> nm.all_hosts()
['127.0.0.1']
>>> nm['127.0.0.1'].hostname()
'localhost'
>>> nm['127.0.0.1'].state()
'up'
>>> nm['127.0.0.1'].all_protocols()
['tcp']
>>> nm['127.0.0.1']['tcp'].keys()
[80, 25, 443, 22, 111]
>>> nm['127.0.0.1'].has_tcp(22)
True
>>> nm['127.0.0.1'].has_tcp(23)
False
>>> nm['127.0.0.1']['tcp'][22]
{'state': 'open', 'reason': 'syn-ack', 'name': 'ssh'}
>>> nm['127.0.0.1'].tcp(22)
{'state': 'open', 'reason': 'syn-ack', 'name': 'ssh'}
>>> nm['127.0.0.1']['tcp'][22]['state']
'open' >>> for host in nm.all_hosts():
>>> print('----------------------------------------------------')
>>> print('Host : %s (%s)' % (host, nm[host].hostname()))
>>> print('State : %s' % nm[host].state())
>>> for proto in nm[host].all_protocols():
>>> print('----------')
>>> print('Protocol : %s' % proto)
>>>
>>> lport = nm[host][proto].keys()
>>> lport.sort()
>>> for port in lport:
>>> print ('port : %s\tstate : %s' % (port, nm[host][proto][port]['state']))
----------------------------------------------------
Host : 127.0.0.1 (localhost)
State : up
----------
Protocol : tcp
port : 22 state : open
port : 25 state : open
port : 80 state : open
port : 111 state : open
port : 443 state : open >>> print(nm.csv())
host;protocol;port;name;state;product;extrainfo;reason;version;conf
127.0.0.1;tcp;22;ssh;open;OpenSSH;protocol 2.0;syn-ack;5.9p1 Debian 5ubuntu1;10
127.0.0.1;tcp;25;smtp;open;Exim smtpd;;syn-ack;4.76;10
127.0.0.1;tcp;53;domain;open;dnsmasq;;syn-ack;2.59;10
127.0.0.1;tcp;80;http;open;Apache httpd;(Ubuntu);syn-ack;2.2.22;10
127.0.0.1;tcp;111;rpcbind;open;;;syn-ack;;10
127.0.0.1;tcp;139;netbios-ssn;open;Samba smbd;workgroup: WORKGROUP;syn-ack;3.X;10
127.0.0.1;tcp;443;;open;;;syn-ack;; >>> nm.scan(hosts='192.168.1.0/24', arguments='-n -sP -PE -PA21,23,80,3389')
>>> hosts_list = [(x, nm[x]['status']['state']) for x in nm.all_hosts()]
>>> for host, status in hosts_list:
>>> print('{0}:{1}'.host)
192.168.1.0:down
192.168.1.1:up
192.168.1.10:down
192.168.1.100:down
192.168.1.101:down
192.168.1.102:down
192.168.1.103:down
192.168.1.104:down
192.168.1.105:down
[...] >>> nma = nmap.PortScannerAsync()
>>> def callback_result(host, scan_result):
>>> print '------------------'
>>> print host, scan_result
>>>
>>> nma.scan(hosts='192.168.1.0/30', arguments='-sP', callback=callback_result)
>>> while nma.still_scanning():
>>> print("Waiting >>>")
>>> nma.wait(2) # you can do whatever you want but I choose to wait after the end of the scan
>>>
192.168.1.1 {'nmap': {'scanstats': {'uphosts': '', 'timestr': 'Mon Jun 7 11:31:11 2010', 'downhosts': '', 'totalhosts': '', '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'}}}
------------------
192.168.1.2 {'nmap': {'scanstats': {'uphosts': '', 'timestr': 'Mon Jun 7 11:31:11 2010', 'downhosts': '', 'totalhosts': '', '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': ''}}}
------------------
192.168.1.3 {'nmap': {'scanstats': {'uphosts': '', 'timestr': 'Mon Jun 7 11:31:11 2010', 'downhosts': '', 'totalhosts': '', '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': ''}}} >>> nm = nmap.PortScannerYield()
>>> for progressive_result in nm.scan('127.0.0.1/24', '22-25'):
>>> print(progressive\_result)

实测代码

没有深入研究,测试着主机的状态有点不理解,比如有些主机ping是不可达的,但是用nmap.state()返回状态是up,不懂原理何在。

 #!/usr/bin/env python
#coding:utf-8 import sys,nmap scan_row = []
userInput = input('>')
scan_row = userInput.split(' ') if len(scan_row) < 2:
print('shuru host and port')
sys.exit() hosts = scan_row[0]
ports = scan_row[1] try:
nm = nmap.PortScanner()
except nmap.PortScannerError:
print("Nmap not found",sys.exc_info())
sys.exit()
except:
print("Unexcepcted error:",sys.exc_info())
sys.exit() try:
nm.scan(hosts, arguments=' -v -sS -p '+ports)
except :
print("Scan error") for host in nm.all_hosts():
print('start'.center(40,'-'))
print('HOST: %s (%s)' % (host,nm[host].hostname()))
print('State: %s' % nm[host].state())
for proto in nm[host].all_protocols():
print('proto'.center(40,'-'))
print('Protocaol: %s' % proto) lport = nm[host][proto].keys()
print(lport)
# lport.sort()
for port in lport:
print('port: %s\tstate:%s' % (port,nm[host][proto][port]['state']))
05-06 22:47