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对象

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']))

automationOperationsWithPython的更多相关文章

随机推荐

  1. sealed(C# 参考)

    sealed 修饰符可以应用于类.实例方法和属性.密封类不能被继承.密封方法会重写基类中的方法,但其本身不能在任何派生类中进一步重写.当应用于方法或属性时,sealed 修饰符必须始终与 overri ...

  2. [backbone] Getting Started with Backbone.js

    一.简介 Backbone 是一个 JavaScript MVC 框架,它属于轻量级框架,且易于学习掌握.模型.视图.集合和路由器从不同的层面划分了应用程序,并负责处理几种特定事件.处理 Ajax 应 ...

  3. iOS的常见文件及程序的启动原理

    一. iOS中常见文件 (一). Xcode6之前 创建项目,默认可以看见一个存放框架的文件夹 info文件以工程文件名开头,如:第一个项目-Info.plist 项目中默认有一个PCH文件 (二). ...

  4. nssm在windows服务器上部署nodejs,coffee启动方式

    本想用forever / pm2 来部署nodejs, 百度后发现只能在Linux系统上使用,window上没法使用,兜一圈后又转nssm了.... 在Linux上,可以轻松的使用forever或者p ...

  5. eclipse 项目修改和更新项目,回退版本,解决分支的冲突的办法

    一个关于git的图 1.我在github建立了3个分支. 2.把其中一个分支拉到本地. 项目修改提交到远程库 3.修改完代码以后commit项目,点击项目右击->team->commit ...

  6. myeclipse 6.5配置tomcat7.X

    软件安装:myeclipse 6.5 Apache Tomcat/7.0.47 安装完成后开始配置 1. 随便展开一个tomcat配置选项,这里配置到tomcat6.x,如下图:

  7. HDU 5768 中国剩余定理

    题目链接:Lucky7 题意:求在l和r范围内,满足能被7整除,而且不满足任意一组,x mod p[i] = a[i]的数的个数. 思路:容斥定理+中国剩余定理+快速乘法. (奇+ 偶-) #incl ...

  8. js中文正则

    正则表达式用于字符串处理.表单验证等场合,实用高效.现将一些常用的表达式收集于此,以备不时之需. 匹配中文字符的正则表达式: [\u4e00-\u9fa5]评注:匹配中文还真是个头疼的事,有了这个表达 ...

  9. CSS 去除列表项li前面的小圆点

    前言 在默认的情况下,浏览器会在<li>标签前面加上小圆点形成列表项,如下所示使用<ul> 项1 项2 项3 或者使用<ol> 项1 项2 项3 这在有时候确实给我 ...

  10. 使用Apache HttpClient

    在一般情况下,如果只是需要Web站点的某个简单页面提交请求并获取服务器响应,完全可以使用前面所介绍的HttpConnection来完成.但在绝大部分情况下,Web站点的网页可能没这么简单,这些页面并不 ...