我正在尝试制作一个端口扫描程序,用于搜索输入端口,该端口是针对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)
答案 0 :(得分:1)
我想你会发现ip地址必须是一个字符串。例如'127.0.0.1'不是int,但端口是int。