测试端口存活
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import time
import socket
def check(ip, port):
print 'checking %s:%d .....' % (ip, port),
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
sock.connect((ip, port))
sock.close()
return True
except Exception, ex:
return False
def main(argv):
fp = open(argv[1], 'r')
for line in fp.readlines():
line = line.rstrip('\r\n')
attrs = line.split(':')
try:
ip = attrs[0]
ports = attrs[1].split(',')
for port in ports:
if check(ip, int(port)): print 'UP!'
else: print 'DOWN!'
except:
print 'ERROR!'
fp.close()
if __name__ == "__main__":
main(sys.argv)
---------------------------------------------------------
恐惧是因为努力的还不够,加油 ~~---------------------------------------------