您的位置:首页 > 编程语言 > Python开发

检测端口状态的python脚本

2016-11-14 10:52 218 查看
#!/usr/bin/env python
import os,subprocess,socket,time,sys
from urllib import urlencode
from socket import gethostname

def check_port():
port = [8091,8080]
failed_port = []
for _each_port in port:
#print i
try:
sk = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sk.settimeout(2)
sk.connect(('localhost',_each_port))
sk.close
now = time.strftime("%Y-%m-%d %H:%M:%S")
tt = "check port success!"
logfile = "/home/op/check.log"
f = open(logfile,'a+')
f.write(now + " " + tt + "\n")
f.close()
except socket.error:
failed_port.append(_each_port)

for _each_port in failed_port:
name = gethostname()
message = "host: %s port: %d down" %(name, _each_port)
tel = [电话号码]
for _each_phone in tel:
data = {
"phone": _each_phone,
"message": message
}
command='curl "http://"短信接口ip":8080/message.php?%s"' %(urlencode(data))
print command
ck = subprocess.Popen(command,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
std_ok, std_err = ck.communicate(input=None)
#print std_ok
#print std_err

if __name__ == '__main__':
check_port()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: