批次檢測主機IP,並把結果生成excel檔案表格

king3171發表於2020-06-19

這是對上一個指令碼的擴充套件,增加了生成excel表格

#coding=gbk

import os

import sys

import xlwt

wk = xlwt.Workbook(encoding = 'utf-8')

sheet1 = wk.add_sheet("IP62",cell_overwrite_ok=True)

style = xlwt.XFStyle()

sheet1.write(0, 0,'ip地址',style)

sheet1.write(0,1, '狀態',style)

x = y = 0

for ip in range(1,255):

    res = os.system('ping -n 1 -w 1 '+sys.argv[1]+str(ip))

    if res == 0:

        print(sys.argv[1]+str(ip)+' up')

        sheet1.write(ip,0,sys.argv[1]+str(ip))

        sheet1.write(ip,1,'up')

        x += 1

    else:

        print(sys.argv[1]+str(ip)+' down')

        sheet1.write(ip,0,sys.argv[1]+str(ip))

        sheet1.write(ip,1,'down')

        y += 1

print('total up ip is '+str(x))

print('total down ip is '+str(y))

sheet1.write(ip+1,0,'total up ip is '+str(x))

sheet1.write(ip+2,0,'total down ip is '+str(y))

wk.save('ip.xls')


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/51077/viewspace-2699438/,如需轉載,請註明出處,否則將追究法律責任。

相關文章