簡體   English   中英

試圖讓網絡掃描儀在樹莓派2(Python)上運行

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

所以我有一個網絡掃描儀,但是它不起作用。 它表明網絡上從127.0.0.1到127.0.0.254的每個IP都已關閉。 我肯定不是這樣。 有人知道問題出在哪里嗎?

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

您的腳本確實可以工作(這很有趣,盡管我更喜歡nmap ;))。

然而

  • do_one帶有第三個參數, psize不知道它如何在您這邊工作。
  • 您需要是root用戶(例如管理員)。 並且您的腳本混合了“失敗”和“失敗”的情況。 當主機關閉時,該函數將返回None 發生錯誤時,表示無法發送ping。 因此, “這表明網絡上從127.0.0.1到127.0.0.254的每個IP都關閉了”這一事實實際上意味着無法發送任何數據包。
  • 127.0.0.1是回送接口 該網絡上沒有其他計算機。 socket.gethostbyname(socket.gethostname())返回該地址的事實可能意味着您的網絡設置不正確。 在過去,127.0.0.1地址被硬鏈接到/etc/hosts文件中的localhost主機名,以寫一些寫得不好的程序(如當時的Gnome應用程序),這些程序將等待與該主機名關聯的IP在繼續啟動之前響應或超時,導致在每次計算機未連接到任何網絡時啟動應用程序的時間都非常長。 IIRC不再需要該解決方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM