python-检测ssh端口
#!/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跟端口。
相关文章