我正在我的家庭网络上运行一个端口扫描仪。如果我在家庭网络上的设备上运行它,它只能在其他Linux机器(物理机器,而不是VM)上工作。所谓“工作”,我的意思是,它发现实际打开的端口很少(ssh、mysql、sunrpc和其他几个端口)。
当扫描Windows和其他杂项物联网设备时,它只是挂起,永远不会完成。我似乎不知道它是在哪里卡住的。
我认为这可能是守则中最相关的部分:
for port in range(begin, end):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
status = s.connect_ex((self.target_ip, port))
except Exception:
continue
finally:
s.close()
if status != 0:
continue
try:
service = socket.getservbyport(port)
except Exception:
service = "unknown"
print("SERVICE: %-15s\tPORT: %-8d" % (service, port))发布于 2019-07-31 00:40:07
我修改了您的代码,以便能够在我的机器上模拟运行,它似乎挂起了,因为没有到达最后一个print语句。但是这是因为你的continue行在if status != 0中总是以“Not0”的形式出现,至少在我的Windows10Pro机器上是这样的。
for port in range(begin, end):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
status = s.connect_ex((self.target_ip, port))
print("DEBUG: ", status) # add this
except Exception as err:
print("Error: ", err) # add this
continue
finally:
s.close()
if status != 0:
print("DEBUG: status was not 0") # add this
# if this line is reached, we start at the top of the loop
# skipping the try except and print below entirely.
continue
try:
service = socket.getservbyport(port)
except Exception:
service = "unknown"
print("SERVICE: %-15s\tPORT: %-8d" % (service, port))你能不能试一试,看看是否会照亮什么可能是错误的,或者让我们知道你得到了什么样的输出。目前,我无法在任何Linux机器或IoT设备上运行该程序。我假设您能够读取每个设备上的控制台输出,从而确定它似乎是“挂起”的。
编辑1:挂起问题的更多上下文
让我举几个例子,说明这个脚本在我的网络上发生了什么。
CASE 1: '192.168.1.0'这个地址存在,但通常不会被使用或分配给任何东西。我的每个端口挂着20秒左右。
案例2: '192.168.1.1'这通常是网络上的路由器。立即回应。被扫描的端口挂起大约1秒。
CASE 3: '192.168.1.3'此设备在网络范围内,但没有设备正在积极使用它。每个端口挂起大约20秒(就像案例1一样)。
所以,长时间的“挂”并不是因为它不起作用。它基本上意味着IP是错误的,或者没有达到设备,所以这个套接字连接尝试达到了超时限制,而不是引发一个异常,它只是向前移动。
编辑2
在迭代大量IP地址和每个正在测试的IP地址的不同端口之前。有时,通过定义对几个端口和特定IP地址的更多控制,一次测试假设1是有帮助的。
这就是我把你的代码转换成的。如果conn不是0,我只是考虑端口关闭了,不管本例中返回了哪个错误代码。我们只关心那些使用0进行响应的程序,因为这表明操作成功。
import socket
# Lets test just a few custom ports.
ports = [21, 22, 80, 8080]
# And test a specific IP address to test, 1 at a time.
ip = '192.168.1.1' # change this if needed
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
for port in ports:
# loop through our small custom port range.
try:
conn = sock.connect_ex((ip, port))
if conn == 0:
print("Port [{}] open.".format(port))
else:
print("Port [{}] closed.".format(port))
except Exception as err:
print(err)
continue
try:
service = socket.getservbyport(port)
except Exception:
service = "unknown"
print("SERVICE: %-15s\tPORT: %-8d" % (service, port))
sock.close()https://stackoverflow.com/questions/57281595
复制相似问题