0
import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(10)
ip = input("IP Address: ")

port_list = {1: "TCP Port Service Multiplexer (TCPMUX)", 5: "Remote Job Entry (RJE)", 7: "ECHO", 18: "Message Send Protocol (MSP)", 20: "FTP -- Data", 21: "FTP -- Control", 22: "SSH Remote Login Protocol", 23: "Telnet", 25: "Simple Mail Transfer Protocol (SMTP)", 29: "MSG ICP", 37: "Time", 42: "Host Name Server (Nameserv)", 43: "WhoIs", 49: "Login Host Protocol (Login)", 53: "Domain Name System (DNS)", 69: "Trivial File Transfer Protocol (TFTP)", 70: "Gopher Services", 79: "Finger", 80: "HTTP", 103: "X.400 Standard", 108: "SNA Gateway Access Server", 109: "POP2", 110: "POP3", 115: "Simple File Transfer Protocol (SFTP)", 118: "SQL Services", 119: "Newsgroup (NNTP)", 137: "NetBIOS Name Service", 139: "NetBIOS Datagram Service", 143: "Interim Mail Access Protocol (IMAP)", 150: "NetBIOS Session Service", 156: "SQL Server", 161: "SNMP", 179: "Border Gateway Protocol (BGP)", 190: "Gateway Access Control Protocol (GACP)", 194: "Internet Relay Chat (IRC)", 197: "Directory Location Service (DLS)", 389: "Lightweight Directory Access Protocol (LDAP)", 396: "Novell Netware over IP", 443: "HTTPS", 444: "Simple Network Paging Protocol (SNPP)", 445: "Microsoft-DS", 458: "Apple QuickTime", 546: "DHCP Client", 547: "DHCP Server", 563: "SNEWS", 569: "MSN", 1080: "Socks"}
print("\n")
print("List of commonly used ports: ")

for key in port_list:
    print(key, port_list[key])

list_of_ports = []
for ports in port_list.keys():
    list_of_ports.append(ports)

print("\n")


def scanner(port):
    strport =str(port)
    if s.connect_ex((ip, port)):
        print(strport + " is closed.")
    else:
        print(strport + " is open.")


for current_port in list_of_ports:
    scanner(current_port)

I'm trying to find out which ports are open and which are closed on my website. I've tested the logic with scanner(), it works as intended.

But for some reason when I'm trying to loop through list_of_ports list its logic breaks.

1 Answers1

1

For every connection you have to use new socket

def scanner(ip, port):
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.settimeout(10)

    if s.connect_ex((ip, port)):
        print(port, "is closed.")
    else:
        print(port, "is open.")

    s.close()

EDIT: I found also this answer on Stackoverflow: Can I use the same socket for multiple connections?


Full code with other small changes

import socket

# --- functions ---

def scanner(ip, port):
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.settimeout(10)

    if s.connect_ex((ip, port)):
        print(port, "is closed.")
    else:
        print(port, "is open.")

    s.close()

# --- main ---

port_list = {
    1: "TCP Port Service Multiplexer (TCPMUX)",
    5: "Remote Job Entry (RJE)",
    7: "ECHO",
    18: "Message Send Protocol (MSP)",
    20: "FTP -- Data",
    21: "FTP -- Control",
    22: "SSH Remote Login Protocol",
    23: "Telnet",
    25: "Simple Mail Transfer Protocol (SMTP)",
    29: "MSG ICP",
    37: "Time",
    42: "Host Name Server (Nameserv)",
    43: "WhoIs",
    49: "Login Host Protocol (Login)",
    53: "Domain Name System (DNS)",
    69: "Trivial File Transfer Protocol (TFTP)",
    70: "Gopher Services",
    79: "Finger",
    80: "HTTP",
    103: "X.400 Standard",
    108: "SNA Gateway Access Server",
    109: "POP2",
    110: "POP3",
    115: "Simple File Transfer Protocol (SFTP)",
    118: "SQL Services",
    119: "Newsgroup (NNTP)",
    137: "NetBIOS Name Service",
    139: "NetBIOS Datagram Service",
    143: "Interim Mail Access Protocol (IMAP)",
    150: "NetBIOS Session Service",
    156: "SQL Server",
    161: "SNMP",
    179: "Border Gateway Protocol (BGP)",
    190: "Gateway Access Control Protocol (GACP)",
    194: "Internet Relay Chat (IRC)",
    197: "Directory Location Service (DLS)",
    389: "Lightweight Directory Access Protocol (LDAP)",
    396: "Novell Netware over IP",
    443: "HTTPS",
    444: "Simple Network Paging Protocol (SNPP)",
    445: "Microsoft-DS",
    458: "Apple QuickTime",
    546: "DHCP Client",
    547: "DHCP Server",
    563: "SNEWS",
    569: "MSN",
    1080: "Socks",
}

ip = input("IP Address: ")

print("List of commonly used ports: ")
for port, name  in port_list.items():
    print(port, name)

for port in port_list:
    scanner(ip, port)

BTW: On Linux you can find common ports with its short names in /etc/services. On Windows should be something similar - probably also file with name services.

Part of this file:

tcpmux      1/tcp               # TCP port service multiplexer
echo        7/tcp
echo        7/udp
discard     9/tcp       sink null
discard     9/udp       sink null
systat      11/tcp      users
daytime     13/tcp
daytime     13/udp
netstat     15/tcp
qotd        17/tcp      quote
msp         18/tcp              # message send protocol
msp         18/udp
chargen     19/tcp      ttytst source
chargen     19/udp      ttytst source
ftp-data    20/tcp
ftp         21/tcp
fsp         21/udp      fspd
ssh         22/tcp              # SSH Remote Login Protocol
furas
  • 134,197
  • 12
  • 106
  • 148
  • Is it possible to optimise this approach to reduce execution time? – Vishrant Khanna Sep 21 '19 at 07:47
  • on local network I use timeout 0.3, eventually 1.0 if WiFi has problem to connect with some computers. Next step would be to write program in C/C++/Rust/Go instead of Python. Or use programs like [nmap](https://nmap.org/) which know different methods to test ports. Maybe on nmap's page you find information how they test ports. In other comment someone mentioned massscan - I never used it but on its page there is comparision with `nmap`. – furas Sep 21 '19 at 08:29