python开发gui网络ping测试_网络工程师使用python实践2_ping测试

背景介绍

在工作中,我们常常会遇到需要进行网络连通性测试,比如:网络割接后,需要ping大量的IP地址,并记录ping的结果。为了避免这种机械的工作,用python写了一个可以自动进行ping测试,并记录下ping结果的脚本。

功能说明

在excel表格中的第一列有需要进行ping测试的IP地址或域名,需要每个IP地址或域名进行ping测试,如果能ping通则在对应第二列的单位格中记录下pass,如果不能ping通则在对应第二列的单位格中记录下fail。

数据表格

EXCEL表格文件名为“ping.xls”,存放测试对象的sheet名称为“Sheet1”,EXCEL表格中的内容如下图:

伪代码

#引入要使用到的模块

import

def get_ping_list(filename, sheet):

#读取excel表格中第一列数据,生成一个需要进行ping测试的列表

def write_to_excel(filename, sheet, ncols, write_result):

#将ping测试的结果写入到excel表格中

def handle_result(result):

#处理ping测试的结果

ping_list = get_ping_list(excel_name, excel_sheet)

for ip in ping_list:

#进行ping测试,并获取ping测试的结果列表

write_to_excel(excel_name, excel_sheet, 1, result_list)

自定义函数的实现

def get_ping_list(filename, sheet):

excel_data = xlrd.open_workbook(filename)

table_device = excel_data.sheet_by_name(sheet)

ping_list = []

for row in range(1,table_device.nrows):

ping_list.append(table_device.row_values(row)[0])

return ping_list

说明:

xlrd.open_workbook(filename),打开excel表格;

excel_data.sheet_by_name(sheet),打开对应的sheet;

for循环,从第一列第二个单元格开始往下读取单元格的内容,将单元格的值加入到ping_list的列表中;

最后返回的是需要进行ping测试的列表。

def write_to_excel(filename, sheet, ncols, write_result):

wb = copy(xlrd.open_workbook(filename))

ws = wb.get_sheet(sheet)

nrows = 0

for result in write_result:

nrows += 1

ws.write(nrows, ncols, result)

wb.save(filename)

说明:

copy(xlrd.open_workbook(filename)),复制原来的excel表格内容;

wb.get_sheet(sheet),打开对应sheet;

for循环,将ping测试的结果一一对应的写入到excel表格中的第二列;

wb.save(filename),将写入结果保存。

def handle_result(result):

if result:

return 'fail'

else:

return 'pass'

说明:

ping测试用到的是os.system('ping x.x.x.x'),这一函数中如果能ping通会返回数值0,如果不能ping通会返回非0,根据这一规划,将ping测试的结果替换成更读性的表达;

if为非0,则返回fail;if为0,则返回pass。

result_list = []

for ip in ping_list:

result = os.system('ping -n 2 -w 1 %s' % ip)

result_list.append(handle_result(result))

说明:

for循环,针对pinglist中的元素,每个元素都进行ping测试;os.system('ping -n 2 -w 1 %s' % ip),为了让加快ping测试,所以设置了ping测试的次数为2,等待回复的超时时间为1毫秒,返回的结果为数值,在上面已经讲过了返回结果的规则;

最后是生成一个result_list记录所有ping测试结果。

完整代码

import os, time, sys

import xlrd, xlwt

from xlutils.copy import copy

def get_ping_list(filename, sheet):

excel_data = xlrd.open_workbook(filename)

table_device = excel_data.sheet_by_name(sheet)

ping_list = []

for row in range(1,table_device.nrows):

ping_list.append(table_device.row_values(row)[0])

return ping_list

def write_to_excel(filename, sheet, ncols, write_result):

wb = copy(xlrd.open_workbook(filename))

ws = wb.get_sheet(sheet)

nrows = 0

for result in write_result:

nrows += 1

ws.write(nrows, ncols, result)

wb.save(filename)

def handle_result(result):

if result:

return 'fail'

else:

return 'pass'

excel_name = 'ping.xls'

excel_sheet = 'Sheet1'

ping_list = get_ping_list(excel_name, excel_sheet)

result_list = []

for ip in ping_list:

result = os.system('ping -n 2 -w 1 %s' % ip)

result_list.append(handle_result(result))

write_to_excel(excel_name, excel_sheet, 1, result_list)

其它

当然,如果不需要这些进行ping测试结果的记录,其它也可以使用群ping工具,比如PingInfoView等,将ping表导入软件,即可直观看到ping测试的结果,如下图所示:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值