I have a port scanner script that scans ports and tells you if they are open or closed. Is there a way I can see the IP addresses that the ports are communicating with? The script:
from threading import Thread
import socket
host = str(input('host > '))
from_port = int(input('start scan from port > '))
to_port = int(input('finish scan to port > '))
counting_open = []
counting_close = []
threads = []
def scan(port):
s = socket.socket()
result = s.connect_ex((str(host),port))
print(('checking ports > '+(str(port))))
if result == 0:
counting_open.append(port)
print((str(port))+' -> is open')
peer = s.getpeername()
print(peer)
s.close()
else:
counting_close.append(port)
#print((str(port))+' -> is closed')
s.close()
for i in range(from_port, to_port+1):
t = Thread(target=scan, args=(i,))
threads.append(t)
t.start()
[x.join() for x in threads]
print(counting_open)
EDIT: Just to be clear, I wasn't asking for the IP of the local host, that is inputted by the user. I was asking if there was a way to know which external public IP's are communicating with the host through the ports found out after the script is run.