Reputation: 81
I have the following code written in 2.7 python:
#...import stuff
remoteServer = raw_input("Enter a remote host to scan: ")
remoteServerIP = socket.gethostbyname(remoteServer)
print "Please wait, scanning remote Host", remoteServerIP
try:
for port in xrange(1, 1024):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
result = sock.connect_ex((remoteServerIP, port))
if result == 0:
print "port {}: open".format(port)
sock.close
except KeyboardInterrupt:
print "\nexiting..."
sys.exit()
Output:
Enter a remote host to scan: www.myexamplesite.com
Please wait, scanning remote Host xxx.xxx.xx.xx
port 21: open
port 22: open
...
But the problem is that I also want to know which ports are used and for what they are used just like:
#... as usual port 1 httpserver port 2 chat server ...
but this is only printing the ports from 1 to 1024 is there a function/way to do this?
Upvotes: 3
Views: 1075
Reputation: 798456
socket.getservbyport()
will translate port numbers into the service expected to be running on that port (via /etc/services
), but won't actually communicate over the port to find out what is really running.
Upvotes: 2