I have a simple Python program I am currently developing and for some reason, it won't work.
The way the full program works is it has a list of 20 ports and it then loops through that list basically pinging the host on the selected port. Here's the code:
import socket
import sys
print ' +-====================================================-+'
print ' / \ '
print '| PyPortScanner |'
print '| by Ag3ntChr0m |'
print ' \ /'
print ' +-====================================================-+'
print ''
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error, msg:
print 'Failed to create socket. Error code: ' + str(msg[0]) + 'Error message: ' + msg[1]
sys.exit()
print 'Socket Created'
host = raw_input('Enter the desired host to scan: ')
port = [80, 443, 21, 22, 4567, 8080, 25, 3389, 23, 53, 1723, 110, 135, 445,
139, 1863, 143, 8081, 10000, 1025]
portFail = []
print 'Scanning top 20 most often open ports ...'
try:
remote_ip = socket.gethostbyname( host )
except socket.gaierror:
#couldn't resolve host at port
print 'Hostname could not be resolved. Program exiting'
sys.exit()
print 'IP address of ' + host + ' is ' + remote_ip
print '+-===================================-+'
print '| Ports Scanned:----------------------|'
print '+-===================================-+'
print ''
#Connect to remote server
for i in range(0, 20):
portScan = int(str(port[i])) <----
try:
s.connect((remote_ip, portScan))
print "\t" + str(portScan)
s.close()
except:
portFail.append(portScan)
err = True
raw_input('Press Enter to Continue...')
if err:
print '+-=============================-+'
print '| Failed Port Scan:-------------|'
print '+-=============================-+'
print ''
size = len(portFail)
for i in range(1, size):
print "\t" + str(portFail[i])
When you run the program it then is supposed to
- load (at the line marked by the arrow) the port number into a variable the s.socket can use to attempt a connection.
- It then attempts to open a socket on the host (basically ping it) and then close it.
- If it connects it writes this on the screen under Ports Scanned.
- However, if it fails (and it has been lately) it gets printed under Failed Port Scan.
When I go to run the program it prints the first port on the list (80) under Ports Scanned, but the rest get placed under Failed Port Scan - even though I know at least some of they are open.
How would I get this program ping more ports than the first successfully?