如何在网络上查找未使用的IP地址?

我只是想在网络上找出未使用的IP地址。 我认为使用nmap是可能的。 请问有人跟我说话吗?

注意:

我只需要免费的IP列表。

快速扫描程序是arp-scan ,它使用ARP “查看”网络上的其他计算机。 它还返回MAC地址并尝试确定网络适配器的制造商。

示例用法(如果需要,用eth0替换wlan0 ):

 $ sudo arp-scan -I wlan0 192.168.1.0/24 Interface: wlan0, datalink type: EN10MB (Ethernet) Starting arp-scan 1.6 with 256 hosts (http://www.nta-monitor.com/tools/arp-scan/) 192.168.1.10 00:90:f5:33:e2:f2 CLEVO CO. 192.168.1.254 00:14:7f:72:cd:05 Thomson Telecom Belgium 2 packets received by filter, 0 packets dropped by kernel Ending arp-scan 1.6: 256 hosts scanned in 1.406 seconds (182.08 hosts/sec). 2 responded 

请注意,此实用程序仅报告已启动的计算机。 ping可以被阻止,但是不能阻止arp-scan因为机器必须与网络上的其他机器进行交互。 为了确保IP未被使用,您最好查看路由器(用于静态/动态地址)和DHCP服务器(用于动态地址)。

sudo nmap -sP -PR 192.168.0.* (或者无论你的网络是什么)都可以解决问题。

要安装它,请使用sudo apt-get install nmap

来源: serverfault.com 。

刚试过这个,就像包含模糊主机的魅力一样,你需要添加sudo才能使用-PR选项。

我发现fping很有用; 除此之外,它还将ping一系列地址并列出“活着”和“无法访问”的地址。 默认情况下不安装fping。

 sudo apt-get install fping 

简单的方法是在一系列地址上运行它。

 fping -g 192.168.0.2 192.168.0.254 2>/dev/null 

更复杂的是,产生一个未使用的IP列表。

 fping -g 192.168.0.2 192.168.0.254 2>/dev/null | grep 'is unreachable' | cut -d ' ' -f 1 | sort -t '.' -k 4 -n 

我相信这不是最好的解决方案,但它可以满足您的需求。 此脚本在192.168.0.0/24网络上运行ping ,如果ARP缓存中没有,则返回非活动IP列表。

优于以前的解决方案:

  • 使用两种方法:ping和ARP检查
  • 无需以root用户身份运行
  • 在我的Core i3-2100上运行大约1.5分钟

要扫描您的网络,请使用 参数运行它。

 #!/usr/bin/env python from threading import Thread import subprocess from Queue import Queue verbose = False num_threads = 8 queue = Queue() inactive_ips = [0 for i in range(256)] lines = open("/proc/net/arp", "r").readlines() arp_cache = [l.split()[0] for l in lines[1:] if l.split()[2] == "0x2"] def ip_str_to_int(ip): ip = ip.rstrip().split('.') ipn = 0 while ip: ipn = (ipn << 8) + int(ip.pop(0)) return ipn def ip_int_to_str(ip): ips = '' for i in range(4): ip, n = divmod(ip, 256) ips = str(n) + '.' + ips return ips[:-1] ## take out extra point #wraps system ping command def pinger(i, q): while True: ip_num = q.get() ip = ip_int_to_str(ip_num) if ip not in arp_cache: ret = subprocess.call("ping -c 1 %s" % ip, shell=True, stdout=open('/dev/null', 'w'), stderr=subprocess.STDOUT) if ret != 0: inactive_ips[ip_num % 256] = ip q.task_done() if __name__ == '__main__': from optparse import OptionParser usage = "usage: %prog [options] [first IP] [last IP]" parser = OptionParser(usage=usage) parser.add_option("-v", "--verbose", action="store_true", dest="verbose", help="make lots of noise") parser.add_option("-q", action="store_false", dest="verbose", help="print only IP adresses") (options, args) = parser.parse_args() verbose = options.verbose first = ip_str_to_int(args[0] if len(args) > 0 else "192.168.0.1") last = ip_str_to_int(args[1] if len(args) > 1 else "192.168.0.254") if verbose: print "Scanning inactive network addresses from %s to %s" % ( ip_int_to_str(first), ip_int_to_str(last)) for i in range(num_threads): worker = Thread(target=pinger, args=(i, queue)) worker.setDaemon(True) worker.start() for ip in range(first, last + 1): queue.put(ip) queue.join() for ip in inactive_ips: if ip: print ip 

downvote后更新

我写的是因为nmap -PR 192.168.0.*对我不起作用:

 Starting Nmap 5.21 ( http://nmap.org ) at 2011-10-06 15:34 EEST Nmap done: 256 IP addresses (0 hosts up) scanned in 0.03 seconds 

更新2

修复了ARP缓存的所有问题。

这应该在bash中正确执行:

 #!/bin/bash #setting language variables for subshell making sure we grep for the right word LC_ALL=C LANG=C # retrieve IP from user input read -p "Input your network (example: 192.168.0): " my_net for i in $(seq 1 254); do ip="$my_net.$i" check="$(ping -c1 "$ip")" if [ "$(grep "Unreachable" <<<"$check")" != "" ] then echo "$ip is unreachable" fi done 

我认为这更简单

 # my_net define my Net_ID my_net=192.168.1. for i in `seq 1 254`; do ip="$my_net$i" ping -c2 $ip | grep "is unreachable" | cut -d" " -f1 & done