发布时间:2019-09-08 09:10:08编辑:auto阅读(1959)
#!/usr/bin/env python
#-*- coding:utf-8 -*-
import socket,sys
from optparse import OptionParser
stat_ok=0
stat_warning=1
stat_critical=2
stat_unknown=3
def check_port(address,port):
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.settimeout(3)
try:
s.connect((address,port))
print "Check ssh port %s OK" %port
sys.exit(stat_ok)
except Exception,e:
print "Check ssh port %s failed:%s" %(port,e)
sys.exit(stat_critical)
def work():
p=OptionParser()
p.add_option("-a","--address",dest="address",default="127.0.0.1",help="address for nagios server",metavar="address")
p.add_option("-p","--port",dest="port",type="int",default="22",help="port for nagios server",metavar="port")
(options,args)=p.parse_args()
result=check_port(options.address,options.port)
if __name__ == "__main__":
work()
备注:
1、使用socket模块进行类似telnet ip port方式检测;
2、使用OptionParser模块增加可选的选项,默认localhost、22端口;-a跟ip,-p跟端口。
上一篇: Centos6.8安装python环境
下一篇: python---骰子游戏
47854
46416
37304
34750
29325
25984
24931
19960
19555
18043
5802°
6426°
5942°
5971°
7075°
5922°
5956°
6450°
6414°
7793°