使用python编写一个统计局域网未关机的脚本并生成excel表格(示例代码)

这篇文章介绍使用python编写一个统计局域网未关机的脚本并生成excel表格(示例代码)

#!/udict/bin/env python
# -*- coding: utf_8 -*-
#date:2016/10/17
#author:wangpeng
#blog:http://wangpengtai.blog.51cto.com
import subprocess
import nmap
import time,datetime
import xlrd,xlsxwriter,xlwt
import os,sys
from xlutils.copy import copy
from multiprocessing import pool
def ip_scan(ip):
global nm
p = subprocess.popen(“ping -c 1 -t 1 “+ip,stdin = subprocess.pipe, stdout = subprocess.pipe, stderr = subprocess.pipe, shell = true)
output = p.stdout.read()
#print output
#the local time
dtime = time.strftime(“%y/%m/%d %x”,time.localtime())
nm = nmap.portscanner()
if “100% packet loss” not in output:
nm.scan(ip,arguments=’-o -ss -su -f’)
try:
dict = {‘status’:’up’,’ip’:ip,’os’:str(nm[ip][‘osmatch’][0][‘name’]),’mac’:str(nm[ip][‘vendor’].keys()[0]),’hostname’:str(nm[ip][‘hostnames’][0][‘name’]),’datetime’:dtime}
#print dict
#addresult(dict,filename,table)
#print ‘ip:%s,dict:%s’ %(ip,dict)
except:
try:
dict = {‘status’:’up’,’ip’:ip,’os’:str(nm[ip][‘osmatch’][0][‘name’]),’mac’:”,’hostname’:str(nm[ip][‘hostnames’][0][‘name’]),’datetime’:dtime}
except:
dict = {‘status’:’up’,’ip’:ip,’os’:”,’mac’:str(nm[ip][‘addresses’][‘mac’]),’hostname’:str(nm[ip][‘hostnames’][0][‘name’]),’datetime’:dtime}
print ip
#print “####error!####”
#print dict
#pass
addresult(dict,filename,table)
else:
print ‘ip:%s—>down!’ %ip
dict = {‘status’:’down’,’ip’:ip,’os’:”,’mac’:”,’hostname’:”,’datetime’:dtime}
addresult(dict,filename,table)
def count_rows(filename):
data = xlrd.open_workbook(filename)
table = data.sheets()[0]
nrows = table.nrows
return nrows
#create a excel table
def addresult(dict,filename,table):
#pick up the key from dict and make it title to excel
title = dict.keys()
#sort the key
title.sort()
clo_num = len(dict.keys())
styleboldred = xlwt.easyxf(‘font: color-index red, bold on’)
headerstyle = styleboldred
if not os.path.exists(filename):
wb = xlwt.workbook()
ws = wb.add_sheet(‘count’)
for i in range(clo_num):
ws.write(0,i,title[i],headerstyle)
ws.write(1,i,dict[title[i]])
wb.save(table)
else:
oldwb = xlrd.open_workbook(table,formatting_info = true)
newwb = copy(oldwb)
newws = newwb.get_sheet(0)
num = count_rows(filename)
for i in range(clo_num):
newws.write(num,i,dict[title[i]])
newwb.save(table)
def start():
global filename
global table
t_date = datetime.date.today().strftime(“%y_%m_%d”)
t_name = ‘report_%s.xls’ %(t_date)
filename = r’/home/python/%s’ %(t_name)
ip_list = []
for i in range(1,255):
ip_list.append(‘172.20.113.’+str(i))
#print ip_list
print(“please wait…”)
#计算时间
time_start=time.time()
#创建线程
for ip in ip_list:
# pid = os.fork()
# if not pid:
ip_scan(ip)
#sys.exit()
time_end=time.time()
t=time_end-time_start
print ‘*’*48
print ‘\ntime:’+str(t)+’s’
print ‘scan results have been saved to test.\n’
print ‘*’*48
if __name__ == ‘__main__’:
“””
filename = r’/home/wangpeng/python/test1.xls’
table = ‘test1.xls’
ip_list = [‘172.20.113.57′,’172.20.113.47′,’172.20.113.10’]
for ip in ip_list:
ip_scan(ip)
“””
start()

以上就是使用python编写一个统计局域网未关机的脚本并生成excel表格(示例代码)的详细内容,更多请关注 第一php社区 其它相关文章!

Posted in 未分类

发表评论