Reputation: 11
I'm doing a project for my course that involves creating a port scanner for IP addresses. It involves a user input for a network address that will then generate a list of addresses and a txt file for the ports. I've got it more or less working, but have noticed something strange that I can't explain or find an answer for.
Basically, when I'm testing the network I'm currently on (standard 192.168 home network) it seems to work as intended. It takes time to scan the port and reports whether they're open or closed. For example, my machine will return the correct ports being open while a non-existent machine will list all its ports as closed.
However, if I put in a different network (say 192.169.0.0) it instantly loops through all of the addresses reporting all ports are open, seemingly without scanning them. And that's what doesn't make sense to me, especially since the earlier non-existent machine returns closed as I'd expect.
I've tried googling it and searching around here but couldn't find an answer (one question went unanswered). This is the specific function I'm calling;
def sockCheck(x):
for y in portList:
try:
location = (str(hosts[x]), int(y))
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
c = s.connect_ex(location)
if c == 0:
print("Port "+y+" open")
else:
print("Port "+y+" closed")
print(" ")
s.close()
except:
print("Port "+y+" unavailable")
s.close()
I know it's not great (still learning). For reference, portList
is the list of ports from the imported port.txt
file and hosts[x]
is how I'm looping through my generated IP addresses (that I need to individually check their ports).
I'll post other code if requested.
Upvotes: 1
Views: 296