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,如需转载请自行联系原作者
目录
相关文章
|
11月前
|
人工智能 JavaScript API
零基础构建MCP服务器:TypeScript/Python双语言实战指南
作为一名深耕技术领域多年的博主摘星,我深刻感受到了MCP(Model Context Protocol)协议在AI生态系统中的革命性意义。MCP作为Anthropic推出的开放标准,正在重新定义AI应用与外部系统的交互方式,它不仅解决了传统API集成的复杂性问题,更为开发者提供了一个统一、安全、高效的连接框架。在过去几个月的实践中,我发现许多开发者对MCP的概念理解透彻,但在实际动手构建MCP服务器时却遇到了各种技术壁垒。从环境配置的细节问题到SDK API的深度理解,从第一个Hello World程序的调试到生产环境的部署优化,每一个环节都可能成为初学者的绊脚石。因此,我决定撰写这篇全面的实
2482 67
零基础构建MCP服务器:TypeScript/Python双语言实战指南
|
12月前
|
Python
掌握Python装饰器:轻松统计函数执行时间
掌握Python装饰器:轻松统计函数执行时间
607 76
|
10月前
|
人工智能 自然语言处理 安全
Python构建MCP服务器:从工具封装到AI集成的全流程实践
MCP协议为AI提供标准化工具调用接口,助力模型高效操作现实世界。
1659 1
|
11月前
|
存储 监控 算法
基于 Python 跳表算法的局域网网络监控软件动态数据索引优化策略研究
局域网网络监控软件需高效处理终端行为数据,跳表作为一种基于概率平衡的动态数据结构,具备高效的插入、删除与查询性能(平均时间复杂度为O(log n)),适用于高频数据写入和随机查询场景。本文深入解析跳表原理,探讨其在局域网监控中的适配性,并提供基于Python的完整实现方案,优化终端会话管理,提升系统响应性能。
274 4
|
人工智能 安全 Shell
Jupyter MCP服务器部署实战:AI模型与Python环境无缝集成教程
Jupyter MCP服务器基于模型上下文协议(MCP),实现大型语言模型与Jupyter环境的无缝集成。它通过标准化接口,让AI模型安全访问和操作Jupyter核心组件,如内核、文件系统和终端。本文深入解析其技术架构、功能特性及部署方法。MCP服务器解决了传统AI模型缺乏实时上下文感知的问题,支持代码执行、变量状态获取、文件管理等功能,提升编程效率。同时,严格的权限控制确保了安全性。作为智能化交互工具,Jupyter MCP为动态计算环境与AI模型之间搭建了高效桥梁。
793 2
Jupyter MCP服务器部署实战:AI模型与Python环境无缝集成教程
|
11月前
|
安全 Linux 网络安全
Python极速搭建局域网文件共享服务器:一行命令实现HTTPS安全传输
本文介绍如何利用Python的http.server模块,通过一行命令快速搭建支持HTTPS的安全文件下载服务器,无需第三方工具,3分钟部署,保障局域网文件共享的隐私与安全。
2732 0
|
11月前
|
Python
如何根据Excel某列数据为依据分成一个新的工作表
在处理Excel数据时,我们常需要根据列值将数据分到不同的工作表或文件中。本文通过Python和VBA两种方法实现该操作:使用Python的`pandas`库按年级拆分为多个文件,再通过VBA宏按班级生成新的工作表,帮助高效整理复杂数据。
|
11月前
|
数据采集 数据可视化 数据挖掘
用 Excel+Power Query 做电商数据分析:从 “每天加班整理数据” 到 “一键生成报表” 的配置教程
在电商运营中,数据是增长的关键驱动力。然而,传统的手工数据处理方式效率低下,耗费大量时间且易出错。本文介绍如何利用 Excel 中的 Power Query 工具,自动化完成电商数据的采集、清洗与分析,大幅提升数据处理效率。通过某美妆电商的实战案例,详细拆解从多平台数据整合到可视化报表生成的全流程,帮助电商从业者摆脱繁琐操作,聚焦业务增长,实现数据驱动的高效运营。
|
存储 安全 大数据
网安工程师必看!AiPy解决fscan扫描数据整理难题—多种信息快速分拣+Excel结构化存储方案
作为一名安全测试工程师,分析fscan扫描结果曾是繁琐的手动活:从海量日志中提取开放端口、漏洞信息和主机数据,耗时又易错。但现在,借助AiPy开发的GUI解析工具,只需喝杯奶茶的时间,即可将[PORT]、[SERVICE]、[VULN]、[HOST]等关键信息智能分类,并生成三份清晰的Excel报表。告别手动整理,大幅提升效率!在安全行业,工具党正碾压手动党。掌握AiPy,把时间留给真正的攻防实战!官网链接:https://www.aipyaipy.com,解锁更多用法!

推荐镜像

更多