测试端口存活

#!/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)
posted @ 2020-08-26 20:58  名字很长容易被惦记  阅读(151)  评论(0编辑  收藏  举报