In this test machine, tcp/80 is listening while tcp/4444 is not. (I already verified this with netstat -an)
If I simply pass args.ip value to ip, and forward it to scan() function, the scan result is always accurate.
ip = args.ip
Normal Output:
C:\>python script.py 127.0.0.1
Port number: 80
Connecting to 127.0.0.1:80
OK 127.0.0.1:80
C:\>python script.py 127.0.0.1
Port number: 4444
Connecting to 127.0.0.1:4444
FAIL 127.0.0.1:4444
C:\>
However, when I try to validate IP Address with the following function
ip = ipaddress.ip_address(args.ip)
the scan result is always wrong.
Wrong Output ... tcp/80 is up and running:
C:\>python script.py 127.0.0.1
Port number: 80
Connecting to 127.0.0.1:80
FAIL 127.0.0.1:80
What's wrong with this code?
import socket, argparse, ipaddress
def main():
global ip
parser = argparse.ArgumentParser()
parser.add_argument('-u')
group = parser.add_mutually_exclusive_group()
group.add_argument('-i')
group.add_argument('ip', nargs='?')
args = parser.parse_args()
if args.i:
print("IP Address is .. " + args.i)
elif args.ip:
ip = ipaddress.ip_address(args.ip) # Prob
#ip = args.ip # OK
scan()
elif args.u:
print("URL is " + args.u)
else:
print('No argument provided')
def scan():
port = input('Port number: ')
print('Connecting to %s:%s' % (ip,port))
try:
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((ip, int(port)))
print('OK %s:%s' % (ip, port))
except:
print('FAIL %s:%s' % (ip, port))
main()
Would it be possible to validate the IP Address and then forward it to another function and still getting the correct port scanning test?