Skip to content

Instantly share code, notes, and snippets.

@tombasche
Created April 11, 2017 09:53
Show Gist options
  • Save tombasche/6879f55a4864c737a0089fa609db4d0c to your computer and use it in GitHub Desktop.
Save tombasche/6879f55a4864c737a0089fa609db4d0c to your computer and use it in GitHub Desktop.
Python port scanner
import socket
import subprocess
import sys
url = str(sys.argv[1])
remoteIp = socket.gethostbyname(url)
print "Scanning " + url + " (" + remoteIp + ")"
try:
for port in range(1,5000):
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
result = sock.connect_ex((url, port))
if result == 0:
print "Port %d open" % port
else:
print "Port %d closed" % port
sock.close()
except Exception, e:
pass
except KeyboardInterrupt:
print "You pressed Ctrl+C"
sys.exit()
except socket.gaierror:
print 'Hostname could not be resolved. Exiting'
sys.exit()
except socket.error:
print "Couldn't connect to server"
sys.exit()
# usage: python scan.py <hostname>
# warning! takes a f**king age
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment