我无法创建同时扫描IP地址和端口的端口扫描程序。端口扫描部分代码工作正常但我无法正确添加IP地址并使循环工作同步。我要求用户输入前3个八位字节,然后代码将使用第四个八位字节扫描并向前移动数字。输出应该看起来像:
IP 127.0.0.1端口22已关闭 IP 127.0.0.1端口23已关闭 IP 127.0.0.1端口24已关闭 IP 127.0.0.2端口22已关闭 IP 127.0.0.2端口23已关闭 IP 127.0.0.2端口24已关闭 ....等
以下是目前的代码:
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server = 'google.com'
portstart = int(input("Enter starting port number to scan: "))
portend = int(input("Enter end port number to scan: "))
octets = input("Enter the first 3 octets of an IP to scan:")
fourthoctet = 1
for portnum in range (portstart, portend):
try:
s.connect((server,portnum))
print("port",portnum,"is open")
except:
print("port",portnum,"is closed")
for fourthoctet in range (1,256):
print("IP", octets,".",fourthoctet)
答案 0 :(得分:0)
根据您想要的输出判断,您的循环不应该像这样吗?
for fourthoctet in range (1,256):
for portnum in range (portstart, portend):
print("IP", octets,".",fourthoctet, end=" ")
try:
s.connect((server,portnum))
print("port",portnum,"is open")
except:
print("port",portnum,"is closed")