Skip to content

Instantly share code, notes, and snippets.

@stevommmm
Created April 15, 2013 23:11
Show Gist options
  • Select an option

  • Save stevommmm/5392049 to your computer and use it in GitHub Desktop.

Select an option

Save stevommmm/5392049 to your computer and use it in GitHub Desktop.
Check if one in multiple ports are active
#!/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