监控服务器端口状态

import socket
import re
import sys
import getopt
import string
def check_server(address,port):
   s=socket.socket()
   print "Attempting to connect to %s on port %s" % (address, port)
   try:
       s.connect((address,port))
       print "Connected to %s on port %s" % (address, port)
       return True
   except socket.error, e:
      print "Connection to %s on port %s failed: %s" %(address, port,e)
      return False
if __name__=="__main__":
   try:
    opts,args=getopt.getopt(sys.argv[1:],"i:p:",["help","ip=","port="])
   except getopt.GetoptErrir as err:
     print(err)
     sys.exit(2)
   ip="null"
   port="null"
   for o,a in opts:
     if o in ("-i","--ip"):
      ip=a
     elif o in ("-p","--port"):
      port=a
     elif o in ("-h","--help"):
      print "help"
     else:
         print "nihao"
   print opts
   check=check_server(ip,string.atoi('80'))
   print "check_server returned %s" % check
   sys.exit(not check)

 

posted @ 2013-04-03 15:30  glose  阅读(341)  评论(0编辑  收藏  举报