Created
April 15, 2013 23:11
-
-
Save stevommmm/5392049 to your computer and use it in GitHub Desktop.
Check if one in multiple ports are active
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| import logging | |
| import socket | |
| import sys | |
| logging.basicConfig(level=logging.INFO) | |
| def portcheck(host, port): | |
| try: | |
| s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
| s.connect((host, port)) | |
| logging.debug("Connected to %s on port %d", host, port) | |
| s.close() | |
| return True | |
| except: | |
| logging.debug("Failed to connect to %s on port %d", host, port) | |
| return False | |
| def handleargs(args): | |
| args = args[1:] | |
| yield socket.gethostbyname(args[0]) | |
| for x in args[1:]: | |
| yield int(x) | |
| if __name__ == '__main__': | |
| n = handleargs(sys.argv) | |
| host = n.next() | |
| p = lambda x: portcheck(host, x) | |
| any([p(x) for x in n]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment