我在創建可同時掃描IP地址和端口的端口掃描器時遇到問題。代碼的端口掃描部分工作正常,但我無法正確添加IP地址並使環路同步工作。我要求用戶輸入前3個八位字節,然後代碼將使用第四個八位字節掃描並向前移動編號。輸出應該看起來像:創建IP和端口掃描器Python
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)
就不是你的端口打印第一則基本上是「IP'octets'。(1-255)」 –
是的,他們打印出來與我現在有代碼的方式的方式。此分配的指示如下:首先提示用戶輸入IP地址的前3個八位位組。例如:127.0.0然後,構建一個允許掃描多個IP地址的外部循環。我們將一直在掃描255個不同的IP地址。 我們將掃描開始與輸入的第一八位位組3的所有IP,例如: 127.0.0.1 127.0.0.2 127.0.0.3 127.0.0.254 在我們將掃描所有254點第四個八位字節的可能性 –
然後換句話說是不這個問題真的只是你有錯誤的順序你的端口和第四個回合循環? –