Python检查ping终端
来源:互联网 发布:仿淘宝网站源码 编辑:程序博客网 时间:2024/06/05 06:21
菜鸟一枚,写着试了试,虽说有点杂乱,但还是能用,我是在linux下运行的
大致说下过程:
1、把需要ping的网段中所有ip存到数组中(我是放到数组中了,其实直接for循环,一个个的也行)
2、遍历数组,逐个ping
3、根据ping返回的字符串,判断是否ping通
4、结果存入txt中
下面上代码咯(其实可以简化代码的,我这里就不简化了)
#!/usr/bin/env python# coding: utf8import timeimport subprocessimport codecsimport osimport re# telnet hostdef pingComputer(host, statusFile):status1 = 'ping success'status2 = 'ping faild'errorStr = 'Destination'for ipAdd in host:print ("get: " +ipAdd + " status")# get now timenowTime = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))p = os.popen("ping -q -c 2 -r " + ipAdd)line = p.read()# judge errorstr in line ifif errorStr in line:writeToText(nowTime, ipAdd, status2, statusFile)else:writeToText(nowTime, ipAdd, status1, statusFile)# write status information to txtdef writeToText(nowTime, ipAdd, status, statusFile):s_text = 'TIME:' + nowTime + '\t' + 'IP:' + ipAdd + '\t' + 'STATUS:' + status + '\r\n'if '0' == judgeFile(statusFile):with open(statusFile, 'a') as f:f.write(s_text)f.close()if '1' == judgeFile(statusFile):with open(statusFile, 'w') as f:f.write(s_text)f.close()# Determine whether statusFile exists # 0: exists# 1: no existsdef judgeFile(statusFile):if os.path.exists(statusFile):return '0'else:return '1'if __name__ == "__main__":IpFirst = '192.168.1.'# ip:1~254host = []for j in range(254): host.append(IpFirst + str(j + 1))# write filestatusFile = '/root/UpStatus.txt'pingComputer(host, statusFile)
就是一台一台的ping,判断,有点慢!
(*^__^*) 嘻嘻……
阅读全文
1 0
- Python检查ping终端
- 局域网ping检查【python】
- python下使用ping检查网络连通情况
- 终端如何Ping网络
- ping检查连接
- 检查能否ping通
- python ping
- python ping
- Python ping
- python 检查IP是否能ping通,并且分别导入两个文件
- python 检查IP是否能ping通,并且分别导入两个文件
- PING命令检查局域网故障
- 检查判断ping 域名地址
- 局域网中终端之间不能Ping问题解决?
- 使用ping命令检查路由之解惑
- 使用ping命令检查路由之解惑
- 使用ping命令检查路由之解惑
- 用PING 命令检查网络连接
- MySql 里的IFNULL、NULLIF和ISNULL用法
- Hyperldeger Composer 错误记录
- ScrollView属性笔记
- HTTP请求头中的refer字段
- ios实现UITableView 之间的圆角和间隙
- Python检查ping终端
- HTTP请求首部的Host字段
- 初学unity,以后做一些笔记
- Required method destroyItem was not overridden
- Socket(二)基于InputStream和OutputStream实现多人聊天功能
- 对JAVA强引用、软引用、弱引用、虚引用的理解
- scala建议使用常量val,而不使用变量var
- HNUST 1695: 跳格子(简单模拟)
- CSS清除浮动方法及优缺点分析