Use python to compile a script for statistics on the local area network that has not been shut down and generate an excel table (sample code)

Source: Internet
Author: User
This article describes how to use python to compile a script for collecting statistics about the unshutdown of a LAN and generate an excel table (sample code) this article describes how to use python to compile a script for collecting statistics about the unshutdown of a LAN and generate an excel table (sample code)

#! /Udict/bin/env python #-*-coding: utf_8-*-# Date: 2016/10/17 # Author: wangpeng # blog: http://wangpengtai.blog.51cto.comimport Subprocessimport nmapimport time, datetimeimport xlrd, xlsxwriter, xlwtimport OS, sysfrom xlutils. copy import copyfrom multiprocessing import Pooldef 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. loc Altime () nm = nmap. portshares () 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] ['bad']. keys () [0]), 'hostname': str (nm [ip] ['hostnames'] [0] ['name']), 'datetime ': dtime} # print dict # addResult (dict, filename, table) # print 'IP: % s, dict: % s' % (IP, dict) handle T: try: dict = {'status': 'up ',' IP ': ip,' OS ': str (nm [ip] ['osmatch'] [0] ['name']), 'Mac ':'', 'hostname': str (nm [ip] ['hostnames'] [0] ['name']), 'datetime': dtime} hour T: dict = {'status ': 'up', 'IP': IP, 'OS': '', 'Mac': str (nm [ip] ['address'] ['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': 'lowdown', '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_mirrors.xls '% (t_date) filename = r'/home/python/% s' % (t_name) ip_list = [] for I in range (1,255): ip_list.append ('2017. 20. 113. '+ str (I) # print ip_list print ("please wait... ") # calculation time time_start = time. time () # create thread 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 = ['2017. 2017113.57 ', '2017. 2017113.47 ', '2017. 2017113.10 '] for ip in ip_list: ip_scan (ip) "" start ()

The preceding section uses python to compile a script for counting the data that has not been shut down on the LAN and generate an excel table (sample code). For more information, see other related articles in the first PHP community!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.