python脚本统计局域网服务器和pc机的系统信息,并生成excel表格

简介:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/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
     =  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  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  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  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()









本文转自 wangpengtai  51CTO博客,原文链接:http://blog.51cto.com/wangpengtai/1904855,如需转载请自行联系原作者
目录
相关文章
|
8天前
|
安全 Java 数据处理
Python网络编程基础(Socket编程)多线程/多进程服务器编程
【4月更文挑战第11天】在网络编程中,随着客户端数量的增加,服务器的处理能力成为了一个重要的考量因素。为了处理多个客户端的并发请求,我们通常需要采用多线程或多进程的方式。在本章中,我们将探讨多线程/多进程服务器编程的概念,并通过一个多线程服务器的示例来演示其实现。
|
18天前
|
弹性计算 Shell Perl
ecs服务器shell常用脚本练习(二)
【4月更文挑战第1天】shell代码训练(二)
102 1
|
21天前
|
Linux Shell Python
Linux执行Python脚本
Linux执行Python脚本
26 1
|
7天前
|
开发者 索引 Python
实践:如何使用python在网页的表格里抓取信息
实践:如何使用python在网页的表格里抓取信息
|
7天前
|
存储 弹性计算 Shell
ecs服务器shell常用脚本练习(十)
【4月更文挑战第11天】shell代码训练(十)
137 0
|
7天前
|
弹性计算 Shell Go
ecs服务器shell常用脚本练习(九)
【4月更文挑战第10天】shell代码训练(八)
124 0
|
10天前
|
JSON 测试技术 持续交付
自动化测试与脚本编写:Python实践指南
【4月更文挑战第9天】本文探讨了Python在自动化测试中的应用,强调其作为热门选择的原因。Python拥有丰富的测试框架(如unittest、pytest、nose)以支持自动化测试,简化测试用例的编写与维护。示例展示了使用unittest进行单元测试的基本步骤。此外,Python还适用于集成测试、系统测试等,提供模拟外部系统行为的工具。在脚本编写实践中,Python的灵活语法和强大库(如os、shutil、sqlite3、json)助力执行复杂测试任务。同时,Python支持并发、分布式执行及与Jenkins、Travis CI等持续集成工具的集成,提升测试效率和质量。
|
11天前
|
弹性计算 Shell Linux
ecs服务器shell常用脚本练习(六)
【4月更文挑战第4天】shell代码训练(六)
108 0
|
11天前
|
Python
Python网络编程基础(Socket编程)UDP服务器编程
【4月更文挑战第8天】Python UDP服务器编程使用socket库创建UDP套接字,绑定到特定地址(如localhost:8000),通过`recvfrom`接收客户端数据报,显示数据长度、地址和内容。无连接的UDP协议使得服务器无法主动发送数据,通常需应用层实现请求-响应机制。当完成时,用`close`关闭套接字。
|
16天前
|
弹性计算 Shell 应用服务中间件
ecs服务器shell常用脚本练习(四)
【4月更文挑战第4天】shell代码训练(四)
96 0