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.

4
  • EDIT: stackoverflow.com/questions/9481419/… Commented Oct 3, 2017 at 1: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ᴘᴇᴇᴅ Commented Oct 3, 2017 at 1:06
  • Get the local IP first, and then get the public IP? Commented Oct 3, 2017 at 1: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? Commented Oct 3, 2017 at 1:10

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

7 Comments

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?
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?
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.
If you could do that, it would be amazing! I've spent over an hour trying to find a solution for this.
If you only need to get the ip address of localhost, you could use a service like 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').
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.