时间:2021-05-22
这个跟ping那个差不多,ping的那个脚本就是通过这个改了下,大体一致,不过telnet的不需要判断返回的字符串。快一些
这里具体需要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通不通的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例讲述了python实现telnet客户端的方法。分享给大家供大家参考。具体如下:python实现的telnet客户端程序,python自带一个telne
速卖通审核不通过。在运营速卖通的过程中,如果发现自己辛辛苦苦上传的宝贝,被判断审核不通过,无疑是一件非常让人头疼的事情,那么,速卖通审核不通过是怎么回事呢?
本文实例讲述了Python实现telnet服务器的方法。分享给大家供大家参考。具体实现方法如下:importthreadingclassmyThread(thr
在Linux中判断一台主机是否可达,可以使用ping命令,而判断端口是否打开,可以使用telnet命令,但是telnet命令没有超时时间的参数,使用起来不是很方
本文实例主要实现的是python根据unicode判断语言类型,具体如下。实例代码:defis_chinese(uchar):"""判断一个unicode是否是