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?
You try to pass an IPv4Address instead of a string to s.connect.
It would be more apparent if you didn't use a catch-all except. This is what you get without it:
thierry@amd:~$ python3 test.py 127.0.0.1
Port number: 80
Connecting to 127.0.0.1:80
Traceback (most recent call last):
File "test.py", line 32, in <module>
main()
File "test.py", line 16, in main
scan()
File "test.py", line 27, in scan
s.connect((ip, int(port)))
TypeError: str, bytes or bytearray expected, not IPv4Address
Just use str(ip) instead of ip:
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((str(ip), int(port))) # ip is an IPv4Address
print('OK %s:%s' % (ip, port)) # no problem here, str will be used for print
except ValueError:
print('FAIL %s:%s' % (ip, port))
thierry@amd:~$ python3 test.py 127.0.0.1
Port number: 80
Connecting to 127.0.0.1:80
OK 127.0.0.1:80
You might also want to change your except: into except ConnectionRefusedError: to avoid to catch all exceptions indistinctly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With