(Python)尝试一次扫描一个IP地址的端口范围时出错

2024-10-03 23:27:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在用户指定的端口范围内一次扫描一个IP地址,直到第四个八位字节达到255。但是,我遇到了一个错误,没有定义名为“count”的变量,尽管我将其定义为=1,因此程序会在每个IP地址(如53.234.12.1、53.234.12.2、53.234.12.3等)中运行

这就是我试图在口译员中展示的东西,毕竟我说了这么多,做了这么多: End Result

这是我的密码:

import socket

server = 'google.com'


startPort = int(input("Enter started port number to scan: "))
endPort = int(input("Enter ending port number to scan: "))
threeOctet = str(input("Enter the first three octets of an IP to scan: "))
countFullIP = threeOctet + "." + str(count)
count = 1

for countFullIP in range(0,256):
    for count in range (startPort,endPort):
        try:
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            s.connect((server,countFullIP))
            print('IP',countFullIP)
        except:
            print('IP is invalid')
        try:
             print('Port',count,'is open')
        except:
             print('Port',count,'is closed')

任何协助都将不胜感激。谢谢你


Tags: toipinputscan定义serverisport
1条回答
网友
1楼 · 发布于 2024-10-03 23:27:17

更换两条管路

countFullIP = threeOctet + "." + str(count)
count = 1

count = 1
countFullIP = threeOctet + "." + str(count)

如您所见,count在赋值之前被引用

根据评论中提到的附加要求更新规范

import socket

server = 'google.com'

startPort = int(input("Enter started port number to scan: "))
endPort = int(input("Enter ending port number to scan: "))
threeOctet = str(input("Enter the first three octets of an IP to scan: "))

for count in range(0,256):
    countFullIP = threeOctet + "." + str(count)
    for count in range (startPort,endPort):
        try:
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            s.connect((server, countFullIP))
            print('IP',countFullIP)
        except:
            print('IP is invalid', countFullIP)
        try:
             print('Port',count,'is open')
        except:
             print('Port',count,'is closed')

相关问题 更多 >