Python檢查ping終端的方法
菜鳥(niǎo)一枚,寫(xiě)著試了試,雖說(shuō)有點(diǎn)雜亂,但還是能用,我是在linux下運(yùn)行的
大致說(shuō)下過(guò)程:
1、把需要ping的網(wǎng)段中所有ip存到數(shù)組中(我是放到數(shù)組中了,其實(shí)直接for循環(huán),一個(gè)個(gè)的也行)
2、遍歷數(shù)組,逐個(gè)ping
3、根據(jù)ping返回的字符串,判斷是否ping通
4、結(jié)果存入txt中
下面上代碼咯(其實(shí)可以簡(jiǎn)化代碼的,我這里就不簡(jiǎn)化了)
#!/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)
就是一臺(tái)一臺(tái)的ping,判斷,有點(diǎn)慢!
以上這篇Python檢查ping終端的方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- Pycharm中安裝wordcloud等庫(kù)失敗問(wèn)題及終端通過(guò)pip安裝的Python庫(kù)如何添加到Pycharm解釋器中(推薦)
- Python在終端通過(guò)pip安裝好包以后在Pycharm中依然無(wú)法使用的問(wèn)題(三種解決方案)
- Python終端輸出彩色字符方法詳解
- python paramiko遠(yuǎn)程服務(wù)器終端操作過(guò)程解析
- 使用python模擬命令行終端的示例
- 在PyCharm的 Terminal(終端)切換Python版本的方法
- python隱藏終端執(zhí)行cmd命令的方法
- 淺談終端直接執(zhí)行py文件,不需要python命令
- 在Linux命令行終端中使用python的簡(jiǎn)單方法(推薦)
- python如何在終端里面顯示一張圖片
- 用Python編寫(xiě)一個(gè)基于終端的實(shí)現(xiàn)翻譯的腳本
- 在終端啟動(dòng)Python時(shí)報(bào)錯(cuò)的解決方案
相關(guān)文章
Python構(gòu)建XML樹(shù)結(jié)構(gòu)的方法示例
這篇文章主要介紹了Python構(gòu)建XML樹(shù)結(jié)構(gòu)的方法,結(jié)合實(shí)例形式分析了Python創(chuàng)建與打印xml數(shù)結(jié)構(gòu)的實(shí)現(xiàn)步驟與相關(guān)操作技巧,需要的朋友可以參考下2017-06-06
Python實(shí)現(xiàn)的選擇排序算法原理與用法實(shí)例分析
這篇文章主要介紹了Python實(shí)現(xiàn)的選擇排序算法,簡(jiǎn)單描述了選擇排序的原理,并結(jié)合實(shí)例形式分析了Python實(shí)現(xiàn)與應(yīng)用選擇排序的具體操作技巧,需要的朋友可以參考下2017-11-11
python實(shí)現(xiàn)蒙特卡羅模擬法的實(shí)踐
?蒙特卡洛就是產(chǎn)生隨機(jī)變量,帶入模型算的結(jié)果,尋優(yōu)方面,本文主要介紹了python 蒙特卡羅模擬法實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
Python argparse模塊實(shí)現(xiàn)解析命令行參數(shù)方法詳解
argparse 是 python 自帶的命令行參數(shù)解析包,可以用來(lái)方便的服務(wù)命令行參數(shù)。本文將通過(guò)示例和大家詳細(xì)講講argparse的使用,需要的可以參考一下2022-09-09
Python企業(yè)編碼生成系統(tǒng)之主程序模塊設(shè)計(jì)詳解
這篇文章主要介紹了Python企業(yè)編碼生成系統(tǒng)之主程序模塊設(shè)計(jì),包括初始化、界面與邏輯實(shí)現(xiàn)相關(guān)操作技巧,需要的朋友可以參考下2019-07-07
Python3如何解決錯(cuò)誤UnicodeDecodeError
當(dāng)我們使用Python3來(lái)處理文本時(shí),一個(gè)非常常見(jiàn)的問(wèn)題就是UnicodeDecodeError,本文小編就來(lái)深入聊聊這個(gè)錯(cuò)誤是怎么來(lái)的以及怎樣來(lái)解決它吧2025-03-03
python實(shí)現(xiàn)在遍歷列表時(shí),直接對(duì)dict元素增加字段的方法
今天小編就為大家分享一篇python實(shí)現(xiàn)在遍歷列表時(shí),直接對(duì)dict元素增加字段的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-01-01

