【发布时间】:2018-05-13 13:16:33
【问题描述】:
我正在尝试制作一个端口扫描器,它会根据 10-255 范围内的所有奇数 IP 地址搜索输入的端口。
我当前的代码不起作用,我收到此错误;
error str, bytes or bytearray expected, not int
我以为s.connect((int(ipaddress.ip_address(my_net[i])), port)) 会解决这个问题,但它没有。
我错过了什么吗?
我当前的代码如下:
import socket
import ipaddress
import subprocess
import sys
from datetime import datetime
#define the subnet to scan
subnet=input("which subnet are you scanning, please enter in x.x.x ")
my_net =[]
count =0
for i in range(11,255):
if i%2!=0:
my_net.insert(count,(subnet+"." +str(i)))
print("Your selected network is " , subnet , "below are the usable Ip addresses")
#the user is to select the port that will be scanned as a part of the test
port = input("Enter the number of the port you would like to scan ")
# Print a banner with information on which host we are about to scan
print ("-" * 60)
print ("Please wait, scanning network" , subnet ,".0/24")
print ("-" * 60)
#check time now#
t1 = datetime.now()
#output. Confirm if the port is open or closed
for i in range(len(my_net)):
try:
socket.setdefaulttimeout (2)
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((int(ipaddress.ip_address(my_net[i])), port))
banner=s.recv(1024)
print(banner)
except Exception as e:
print("error " , e)
# Checking the time again
t2 = datetime.now()
# Calculates the difference of time, to see how long it took to run the script
total = t2 - t1
print ('Scanning Completed in: ', total)
【问题讨论】:
-
您首先尝试使用
int(ipaddress.ip_address(my_net[i]))实现的目标。为什么不直接使用s.connect((my_net[i],port)),因为`my_net[i] 已经是您要使用的IP 地址了。 -
我尝试使用 ipmodule 的原因是因为我在将代码更改为时收到错误:s.connect((int(my_net[i]), port)) error invalid literal for int() with基数 10:'192.168.1.53'
-
是什么让你认为它首先应该是
s.connect(int(ip,port))?它应该是s.connect((ip,port)),即没有任何类型的int。我建议实际查看文档。 -
感谢@SteffenUllrich,这是我尝试的第一种方式。该错误表明它需要接收一个整数。我会继续努力。感谢您花时间阅读我的帖子。
-
这可能是因为您的端口是一个 str(由
input返回),但您需要一个int,即port = int(input(....))
标签: python sockets python-3.6 port-scanning