国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python檢查ping終端的方法

2020-02-16 00:55:03
字體:
來源:轉載
供稿:網友

菜鳥一枚,寫著試了試,雖說有點雜亂,但還是能用,我是在linux下運行的

大致說下過程:

1、把需要ping的網段中所有ip存到數組中(我是放到數組中了,其實直接for循環,一個個的也行)

2、遍歷數組,逐個ping

3、根據ping返回的字符串,判斷是否ping通

4、結果存入txt中

下面上代碼咯(其實可以簡化代碼的,我這里就不簡化了)

#!/usr/bin/env python# coding: utf8 import 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 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 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~254	host = []	for j in range(254): 		host.append(IpFirst + str(j + 1))	# write file	statusFile = '/root/UpStatus.txt'	pingComputer(host, statusFile)

就是一臺一臺的ping,判斷,有點慢!

以上這篇Python檢查ping終端的方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持武林站長站。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 五大连池市| 修武县| 新泰市| 田林县| 根河市| 蒲江县| 南乐县| 长沙县| 师宗县| 资阳市| 武胜县| 百色市| 嘉祥县| 梨树县| 平遥县| 石渠县| 柏乡县| 奉化市| 内黄县| 泰州市| 叙永县| 宜君县| 都匀市| 灌南县| 仁怀市| 桐城市| 沁阳市| 姜堰市| 柏乡县| 南靖县| 高安市| 泗阳县| 榕江县| 施秉县| 徐汇区| 长泰县| 临沂市| 无锡市| 贡山| 松原市| 凌海市|