I tried making a ssh port scanner in python that takes every ip of a class (for example 13.120.xy). I got no error so far but it takes so long and so far no ip address has the port open (I've been running it for 5 minutes).
EDIT: It seems I do get the message when the port is open.
import socket
from datetime import datetime
from multiprocessing.dummy import Pool as ThreadPool
def make_ips(ip_class):
ip_addresses = [f"{ip_class}.{part3}.{part4}" for part3 in range(0, 256) for part4 in range(0, 256)]
return ip_addresses
def scan(remoteServerIP):
print("-" * 60)
print("Please wait, scanning remote host", remoteServerIP)
print("-" * 60)
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
result = sock.connect_ex((remoteServerIP, 22))
if result == 0:
print(f"{remoteServerIP}: Open")
return remoteServerIP
sock.close()
except socket.gaierror:
print(f'Hostname {remoteServerIP} could not be resolved.')
except socket.error:
print(f"Couldn't connect to server {remoteServerIP}")
def main():
t1 = datetime.now()
ipclass = str(input("please input the ip class: "))
ips = make_ips(ipclass)
hostnames = []
pool = ThreadPool(13)
hostnames = pool.map(scan, ips)
pool.close()
pool.join()
open("hosts.txt", "w").write(hostnames).splitlines()
t2 = datetime.now()
total = t2 - t1
print('Scanning Completed in: ', total)
if __name__ == "__main__":
main()
I fixed it. I had to use more threads.
import socket
from datetime import datetime
from multiprocessing.dummy import Pool as ThreadPool
def make_ips(ip_class):
ip_addresses = [f"{ip_class}.{part3}.{part4}" for part3 in range(0, 256) for part4 in range(0, 256)]
return ip_addresses
def scan(remoteServerIP):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
result = sock.connect_ex((remoteServerIP, 22))
if result == 0:
print(f"{remoteServerIP}: Open")
return remoteServerIP
sock.close()
def main():
ipclass = str(input("please input the ip class: "))
ips = make_ips(ipclass)
pool = ThreadPool(20000)
hostnames = pool.map(scan, ips)
pool.close()
pool.join()
for hostname in hostnames:
if hostname:
open("hosts.txt", "a").write(f"{hostname}\n")
if __name__ == "__main__":
main()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.