python循環監控遠程端口的代碼
在ip.txt中每行一個ip地址和端口號,代碼可循環監控這些ip地址的指定端口是否正常
#!/usr/bin/env python-- coding: gbk --
import socket,time while 1: file_obj = open('ip.txt') for line in file_obj: try: sc=socket.socket(socket.AF_INET,socket.SOCK_STREAM) ip = line.split()[0] port = int(line.split()[1]) print ip,port
#設置超時時間(0.0) sc.settimeout(2) sc.connect((ip,port)) timenow=time.localtime() datenow = time.strftime('%Y-%m-%d %H:%M:%S', timenow) logstr="%s:%s 連接成功->%s \n" %(ip,port,datenow) print logstr sc.close() except: file = open("log.txt", "a") timenow=time.localtime() datenow = time.strftime('%Y-%m-%d %H:%M:%S', timenow) logstr="%s:%s 連接失敗->%s \n" %(ip,port,datenow) print logstr file.write(logstr) file.close() print "sleep 10....." time.sleep(10)</pre>
ip.txt格式192.168.1.100 33001 192.168.1.101 33001
本文由用戶 n6xb 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!