简体   繁体   English

我正在尝试扫描我的网站以检查哪些端口是打开的。 我只是不明白为什么在使用循环时这个逻辑不起作用

[英]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.

相关问题 我无法弄清楚为什么我的 python 脚本可以运行,但是当尝试使其成为可执行文件时它不起作用 - I can't figure out why my python script works but when try to make it an executable it does not work 试图为我的 python 类完成这个程序,但无法弄清楚为什么我会收到 TypeError - Trying to finish this program for my python class, but can't figure out why I'm receiving a TypeError 不知道为什么我没有收到通知 - Can't figure out why I'm not receiving notifications 我正在尝试为我的“情绪分析”项目制作前端,但不知道如何让我的“预测功能”在前端工作 - I'm trying to make Front-end for my “Sentiment Analysis” project but can't figure out how to make my “prediction_function” work at the Front-end 我正在阅读自学程序员 ch17 这本书,但我不明白为什么这不起作用 - I'm reading the book the self taught programmer ch17 and I can't figure out why this dosn't work 我无法弄清楚我的代码不起作用的原因 - I can't figure out the reasoning why my code doesn't work 我计算最大二进制间隙的函数不起作用,但我不知道为什么 - My Function To Count The Largest Binary Gap Doesn't Work But I Can't Figure Out Why 我正在尝试在我的 class 中创建一个用户输入字典以获得额外的信用,但我无法弄清楚如何正确结束程序 - I'm trying to make a user input dictionary for extra credit in my class and I can't figure out how to properly end the program 无法弄清楚为什么我的 if/else 语句不像我想要的那样工作 - Can not figure out why my if/else statement doesn`t work like i intend 无法弄清楚为什么将JSON值导入DF时出错 - Can't figure out why I get error when trying to get JSON value into DF
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM