【问题标题】:How can I get my port scanner to work我怎样才能让我的端口扫描仪工作
【发布时间】: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


【解决方案1】:

我想你会发现 ip 地址必须是一个字符串。例如 '127.0.0.1' 不是 int,但端口是 int。

【讨论】:

  • 如果我将代码更改为; s.connect((int(ipaddress.ip_address(my_net[i])), port)) 我得到错误;错误需要整数(类型为 str)
  • 是的,就像您问题下方的评论者一样,我对您使用“ipaddress.ip_address”深感困惑,这充其量是多余的,而最坏的情况是您的问题的根源。提供 my_net[i] 作为参数应该可以工作。如果您查看docs.python.org/3/howto/sockets.html 此处的示例,您会发现它们都为主机提供了一个 str。
  • 顺便说一下端口确实需要是整数。
  • 这很有帮助 我已将端口输入格式化为 int 并且似乎取得了进展。 port = int(input("Enter the number of the port you want to scan")) 我已经按照 Steffen 删除了 IP 模块 感谢您抽出时间 Paula。对不起,我的这个令人费解的问题,我才编码了几个星期。
  • 您可能还受益于使用 my_net.append(subnet+"." +str(i)) 来获取您所针对的 IP 地址的完整列表,而不仅仅是最后一个。就个人而言,我从不使用插入,但如果你这样做,你将需要更新计数器。此外,您可以使用 range(11,256,2),它会给您 11,13..,255,另外您可能想知道 range 只返回小于其上限的数字,因此 range(0,n) 将返回 0 ,1,,2...,n-1。希望这会有所帮助。
猜你喜欢
  • 2012-10-07
  • 1970-01-01
  • 2021-01-22
  • 2011-04-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-28
  • 2016-07-26
相关资源
最近更新 更多