python循环监控远程端口的方法
作者:chongq
这篇文章主要介绍了python循环监控远程端口的方法,涉及Python实现端口监控的技巧,需要的朋友可以参考下
本文实例讲述了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)
ip.txt格式文件:
192.168.1.100 33001 192.168.1.101 33001
希望本文所述对大家的Python程序设计有所帮助。
您可能感兴趣的文章:
- python爬虫爬取监控教务系统的思路详解
- 基于python的Linux系统指定进程性能监控思路详解
- python实现内存监控系统
- Python系统监控模块psutil功能与经典用法分析
- Python pyinotify日志监控系统处理日志的方法
- python使用wmi模块获取windows下的系统信息 监控系统
- Python写的一个简单监控系统
- linux系统使用python监控apache服务器进程脚本分享
- python监控nginx端口和进程状态
- Python实现数通设备端口使用情况监控实例
- python实现数通设备端口监控示例
- Python3监控windows,linux系统的CPU、硬盘、内存使用率和各个端口的开启情况详细代码实例
