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

首頁 > 編程 > Python > 正文

Python判斷telnet通不通的實例

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

這個跟ping那個差不多,ping的那個腳本就是通過這個改了下,大體一致,不過telnet的不需要判斷返回的字符串??煲恍?/p>

這里具體需要telnet的ip是需要自己向定義好的數組中寫的

我這里加了一個定時,是7200秒,也就是2小時

行了,上代碼吧:

#!/usr/bin/env python# coding: utf8 import telnetlibimport timeimport codecsimport os # telnet hostdef telnetdo(host, statusFile): status1 = 'telnet success' status2 = 'telnet faild' for ipAdd in host: # get now time nowTime = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())) try:  t = telnetlib.Telnet(ipAdd, port=23, timeout=1)  writeToText(nowTime, ipAdd, status1, statusFile) except:  writeToText(nowTime, ipAdd, status2, 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__": host = ['192.168.1.254', '192.168.1.100'] # write file statusFile = '/root/telnetStatus.txt' i = 7200 while i: telnetdo(host, statusFile) time.sleep(2) i = i - 1

結果會存在/root下面

以上這篇Python判斷telnet通不通的實例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持武林站長站。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 奎屯市| 汽车| 抚州市| 钦州市| 洛浦县| 郧西县| 吉安县| 广丰县| 汝城县| 武威市| 阜宁县| 中西区| 封开县| 临西县| 萨嘎县| 子长县| 晴隆县| 桂阳县| 陈巴尔虎旗| 临桂县| 准格尔旗| 宜宾市| 富平县| 二连浩特市| 桃园市| 博客| 台中县| 泽库县| 汽车| 青铜峡市| 锦屏县| 修武县| 乐平市| 阿瓦提县| 德保县| 柯坪县| 新干县| 开平市| 清水河县| 江陵县| 辽阳市|