I am trying to make a port scanner that searches a port that is input against all odd numbered IP address in the range of 10-255.
My current code isn't working, and I am receiving this error;
error str, bytes or bytearray expected, not int
I thought s.connect((int(ipaddress.ip_address(my_net[i])), port))
would fix this but it did not.
Am I missing something?
My current code is below:
import socket
import ipaddress
import subprocess
import sys
from datetime import datetime
#define the subnet to scan
subnet=input("which subnet are you scanning, please enter in x.x.x ")
my_net =[]
count =0
for i in range(11,255):
if i%2!=0:
my_net.insert(count,(subnet+"." +str(i)))
print("Your selected network is " , subnet , "below are the usable Ip addresses")
#the user is to select the port that will be scanned as a part of the test
port = input("Enter the number of the port you would like to scan ")
# Print a banner with information on which host we are about to scan
print ("-" * 60)
print ("Please wait, scanning network" , subnet ,".0/24")
print ("-" * 60)
#check time now#
t1 = datetime.now()
#output. Confirm if the port is open or closed
for i in range(len(my_net)):
try:
socket.setdefaulttimeout (2)
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((int(ipaddress.ip_address(my_net[i])), port))
banner=s.recv(1024)
print(banner)
except Exception as e:
print("error " , e)
# Checking the time again
t2 = datetime.now()
# Calculates the difference of time, to see how long it took to run the script
total = t2 - t1
print ('Scanning Completed in: ', total)
I think you'll find that the ip address has to be a string. For instance '127.0.0.1' not an int, the port is an int though.