简体   繁体   English

试图让网络扫描仪在树莓派2(Python)上运行

[英]Trying to get a network scanner working on raspberry pi 2 (python)

So I have a network scanner, but its not working. 所以我有一个网络扫描仪,但是它不起作用。 It's showing that every ip on the network from 127.0.0.1 to 127.0.0.254 is down. 它表明网络上从127.0.0.1到127.0.0.254的每个IP都已关闭。 I know for sure that's not the case. 我肯定不是这样。 Does anyone know what the problem is? 有人知道问题出在哪里吗?

import socket, ping
from struct import unpack, pack

my_ip = socket.gethostbyname(socket.gethostname())
print "My computer IP address:", my_ip
my_deets = socket.gethostbyname_ex(socket.gethostname())
print "My computer details:", my_deets


subnet_mask = "255.255.255.0"

def ip2long(ip):
    """7
    Convert an IP string to long
    """
    return unpack(">L", socket.inet_aton(ip))[0]

def long2ip(ip):
    """
    Convert a long to IP string
    """
    return socket.inet_ntoa(pack('!L', ip))

# Applying the subnet mask to IP
addr = ip2long(my_ip)
mask = ip2long("255.255.255.0")
prefix = addr & mask

print "Base address for network", long2ip(prefix)

# Get the number of possible computers on our network
all_computers = ip2long("255.255.255.255")
num_computers = all_computers ^ mask

# Go through each computer
for ip_suffix in range(num_computers):
    # Try to ping a computer on the network
    test_ip = long2ip(prefix + ip_suffix)
    try:
        print "[*] Checking to see if host is up..."
        timeout = ping.do_one(test_ip, 1)
        print timeout
        if timeout != None:
            print "[+] Host is there:", test_ip
        print "-"*100
    except socket.error, e:
        print "[-] Host not there:", test_ip

Your script does work (and it's kind of fun, though I'll prefer nmap ;) ). 您的脚本确实可以工作(这很有趣,尽管我更喜欢nmap ;))。

However 然而

  • do_one takes a third argument, psize don't know how it works on your side. do_one带有第三个参数, psize不知道它如何在您这边工作。
  • You need to be root (eg administrator). 您需要是root用户(例如管理员)。 And your script mixes up the "failure" and "host down" cases. 并且您的脚本混合了“失败”和“失败”的情况。 When a host is down, the function will return None . 当主机关闭时,该函数将返回None When an error occurs, it means that the ping could not be sent. 发生错误时,表示无法发送ping。 So the fact that "It's showing that every ip on the network from 127.0.0.1 to 127.0.0.254 is down" , means actually that no packets could be sent. 因此, “这表明网络上从127.0.0.1到127.0.0.254的每个IP都关闭了”这一事实实际上意味着无法发送任何数据包。
  • 127.0.0.1 is the loopback interface . 127.0.0.1是回送接口 There will be no other computer than yours on that network. 该网络上没有其他计算机。 The fact that socket.gethostbyname(socket.gethostname()) returns that address could mean that your network is improperly set. socket.gethostbyname(socket.gethostname())返回该地址的事实可能意味着您的网络设置不正确。 In some old days, the 127.0.0.1 address was hardlinked to the localhost hostname in the /etc/hosts file in order for some badly written programs (like Gnome apps at that time), who would wait for the IP associated with the hostname to respond or timeout before continuing with the start up, leading to extremely long delay to start the application each time the computer wasn't connected to any network. 在过去,127.0.0.1地址被硬链接到/etc/hosts文件中的localhost主机名,以写一些写得不好的程序(如当时的Gnome应用程序),这些程序将等待与该主机名关联的IP在继续启动之前响应或超时,导致在每次计算机未连接到任何网络时启动应用程序的时间都非常长。 That workaround is no longer necessary IIRC. IIRC不再需要该解决方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM