0

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.

GS483
  • 31
  • 4
  • EDIT: https://stackoverflow.com/questions/9481419/how-can-i-get-the-public-ip-using-python2-7 – cs95 Oct 03 '17 at 01:02
  • This post is asking for the machine's own external IP. I'm asking the external IP addresses of what the machine is communicating with on specific ports. @cᴏʟᴅsᴘᴇᴇᴅ – GS483 Oct 03 '17 at 01:06
  • Get the local IP first, and then get the public IP? – cs95 Oct 03 '17 at 01:07
  • I already have the Local IP address, as it's inputted by the user. But for example if I found out that on IP address 192.168.1.45, ports 1, 2, and 3 are open, I would like to know what those ports are communicating with. If it's another computer, maybe the public IP address of that computer? – GS483 Oct 03 '17 at 01:10

1 Answers1

0

Use getpeername

>>> s.getpeername()
('207.38.86.25', 80)

For anyone else coming to this question who wants to get the ip address of a website without creating a socket object first, you can also use socket.gethostbyname(hostname) like so:

def get_ip_address(host):
    try:
        return socket.gethostbyname(host)
    except:
         return None
hostingutilities.com
  • 8,894
  • 3
  • 41
  • 51
  • Thanks, but I didn't mean this. I wanted to know what external IP addresses are being communicated using the specific ports. For example, when I run the script and input the host as 192.168.1.22 and scan ports 1-100, it might say that ports 4,12, and 88 are open. How do I check which IP external addresses are communicating with the host (192.168.1.22) using ports 4,12, and 88? – GS483 Oct 03 '17 at 00:55
  • Thank you, Mr. Me. This still outputs the local host with the open ports. Can you please look at my code to make sure I used s.getpeername() right? – GS483 Oct 03 '17 at 01:03
  • Hmmm. I'm not sure how to get the external IP address. I'll have to do some digging around, and see if I can come up with a solution. – hostingutilities.com Oct 03 '17 at 01:06
  • If you could do that, it would be amazing! I've spent over an hour trying to find a solution for this. – GS483 Oct 03 '17 at 01:07
  • If you only need to get the ip address of localhost, you could use a service like https://ipapi.co/ip/ to grab your external ip address. You would do something like `external_ip = urllib.request.urlopen('http://ident.me').read().decode('utf8')`. – hostingutilities.com Oct 03 '17 at 01:14
  • actually I meant the foreign IP of the port that I am communicating with. Like if my local IP is communicating with, say, a different laptop on port 15, I would need the IP of that laptop. – GS483 Oct 03 '17 at 01:19
  • That's what getpeername() returns. The docs say it "Returns the remote address to which the socket is connected". On a computer on the same network as me this is returning a local ip address, and for computers outside of my network I'm getting the external ip address. Either way, this is the ip address I would use to communicate with the computer. – hostingutilities.com Oct 03 '17 at 02:12