所以我有一台网络扫描仪,但它无法正常工作。它显示从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
答案 0 :(得分:0)
你的脚本确实有用(虽然我更喜欢nmap
,但它很有趣)。
然而
do_one
提出第三个论点,psize
不知道它是如何起作用的。None
。发生错误时,表示无法发送ping。因此,"它显示从127.0.0.1到127.0.0.254的网络上的每个IP都已关闭" ,这意味着实际上没有数据包可以被发送。 socket.gethostbyname(socket.gethostname())
返回该地址的事实可能意味着您的网络设置不正确。在过去的某些时候,127.0.0.1地址被硬链接到/etc/hosts
文件中的localhost主机名,以便编写一些编写得很糟糕的程序(如当时的Gnome应用程序),谁会等待与主机名关联的IP在继续启动之前响应或超时,导致每次计算机未连接到任何网络时启动应用程序的延迟极长。这种解决方法不再是必要的IIRC。