[英]I'm trying to scan my website to check which ports are open. I just can't figure out why this logic does not work when using loops
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.但是由于某种原因,当我尝试遍历 list_of_ports 列表时,它的逻辑中断了。
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?编辑:我在 Stackoverflow 上也找到了这个答案: 我可以使用同一个套接字进行多个连接吗?
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
.顺便说一句:在 Linux 上,您可以在/etc/services
中找到带有短名称的常用端口。 On Windows should be something similar - probably also file with name services
.在 Windows 上应该是类似的东西——也可能是带有名称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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.