Hi All,
I’m new to the forum. I’m working on a function to test ports against a target ip. I am adding what I have for the code below. This the initial code was pulled from an online post after doing a search online. When I run the code, it appears to target the ip address, however it appears that it’s looping through the port number over and over even if I only specify one port. I’m trying to just target one port right now. If I specify port 80 for example, I get 8 and 0 on two separate lines. Any thoughts on where I am off? Thanks!
\
import socket
target = input(‘enter ip address’)
ports_to_scan = input(‘enter port number’)
def port_scanner(target, port):
try:
# Create a socket object
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Set a timeout for the connection
s.settimeout(1)
# Attempt to connect to the target and Ip port
s.connect((target, port))
# Close the socket
s.close()
return True
except:
return False
for port in ports_to_scan:
if port_scanner(target, port):
print(f"Port {port} on {target} is OPEN")
else:
print(f"Port {port} on {target} is CLOSED")
\\