[英]I can't figure out why my python script works but when try to make it an executable it does not work
[英]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)
我試圖找出我的網站上哪些端口是開放的,哪些是關閉的。 我已經用掃描儀()測試了邏輯,它按預期工作。
但是由於某種原因,當我嘗試遍歷 list_of_ports 列表時,它的邏輯中斷了。
對於每個連接,您必須使用新套接字
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()
編輯:我在 Stackoverflow 上也找到了這個答案: 我可以使用同一個套接字進行多個連接嗎?
帶有其他小改動的完整代碼
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)
順便說一句:在 Linux 上,您可以在/etc/services
中找到帶有短名稱的常用端口。 在 Windows 上應該是類似的東西——也可能是帶有名稱services
的文件。
該文件的一部分:
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.