Python检查ping终端的方法
作者:Persistent_ 时间:2023-04-16 07:03:37
菜鸟一枚,写着试了试,虽说有点杂乱,但还是能用,我是在linux下运行的
大致说下过程:
1、把需要ping的网段中所有ip存到数组中(我是放到数组中了,其实直接for循环,一个个的也行)
2、遍历数组,逐个ping
3、根据ping返回的字符串,判断是否ping通
4、结果存入txt中
下面上代码咯(其实可以简化代码的,我这里就不简化了)
#!/usr/bin/env python
# coding: utf8
import time
import subprocess
import codecs
import os
import re
# telnet host
def pingComputer(host, statusFile):
status1 = 'ping success'
status2 = 'ping faild'
errorStr = 'Destination'
for ipAdd in host:
print ("get: " +ipAdd + " status")
# get now time
nowTime = 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 if
if errorStr in line:
writeToText(nowTime, ipAdd, status2, statusFile)
else:
writeToText(nowTime, ipAdd, status1, statusFile)
# write status information to txt
def 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 exists
def judgeFile(statusFile):
if os.path.exists(statusFile):
return '0'
else:
return '1'
if __name__ == "__main__":
IpFirst = '192.168.1.'
# ip:1~254
host = []
for j in range(254):
host.append(IpFirst + str(j + 1))
# write file
statusFile = '/root/UpStatus.txt'
pingComputer(host, statusFile)
就是一台一台的ping,判断,有点慢!
来源:https://blog.csdn.net/qq_21911403/article/details/77583561
标签:Python,ping
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python实现BackPropagation算法
2022-03-26 12:06:39
Minio设置文件链接永久有效的完整步骤
2023-06-10 22:26:10
![](https://img.aspxhome.com/file/2023/7/114717_0s.png)
sql not in 与not exists使用中的细微差别
2024-01-26 09:40:43
Django Rest framework解析器和渲染器详解
2021-06-30 20:46:25
![](https://img.aspxhome.com/file/2023/5/115215_0s.png)
Hadoop介绍与安装配置方法
2022-08-10 03:50:02
Python中Collections模块的Counter容器类使用教程
2021-10-23 07:28:44
![](https://img.aspxhome.com/file/2023/9/70189_0s.png)
python实现简单的飞机大战游戏
2023-08-28 01:50:50
![](https://img.aspxhome.com/file/2023/2/135112_0s.jpg)
深入理解Python3中的http.client模块
2021-05-26 04:12:34
matplotlib绘制两点间连线的几种方法实现
2021-07-27 09:02:24
![](https://img.aspxhome.com/file/2023/0/107240_0s.jpg)
JS实现键值对遍历json数组功能示例
2024-04-10 10:52:40
![](https://img.aspxhome.com/file/2023/6/136886_0s.png)
JavaScript性能优化小技巧,创建文档碎片
2010-03-31 18:27:00
Python爬虫框架Scrapy基本用法入门教程
2021-08-17 19:50:45
![](https://img.aspxhome.com/file/2023/4/111894_0s.png)
Python+SimpleRNN实现股票预测详解
2022-12-04 13:08:26
![](https://img.aspxhome.com/file/2023/8/105778_0s.png)
分析并输出Python代码依赖的库的实现代码
2022-04-05 17:07:23
我的栅格系统实现
2008-09-21 13:50:00
![](https://img.aspxhome.com/file/UploadPic/20089/21/2008921135617161s.jpg)
Oracle数据库性能优化技术开发者网络Oracle
2010-07-18 13:05:00
详解如何利用tushare、pycharm和excel三者结合进行股票分析
2021-12-11 04:04:47
![](https://img.aspxhome.com/file/2023/6/99156_0s.jpg)
支持多风格变换的ASP分页类
2007-10-13 18:48:00
浅谈java里的EL表达式在JSP中不能解析的问题
2023-06-20 11:55:05
python实现推箱子游戏
2023-08-26 14:22:39
![](https://img.aspxhome.com/file/2023/8/121908_0s.jpg)