一个利用arping命令,批量扫描局域网内存活主机的小脚本。

arping命令是linux下一个很好用的局域网主机探测工具,利用arp协议进行探测,相对于ping命令,其明显的优势便是速度快,缺点是仅能进行局域网内探测。但arping命令本身不支持批量探测,所以写了个批量扫描脚本。

Usage: ./arpings interface
Avaliable interface:
	br-133d1db9f98a br-1e4cf58baa3e docker0 eth0 lo  # 脚本会检测并显示本机可用的网卡接口
For example, scanning at eth0 interface: ./arpings eth0

执行脚本后,会显示扫描到的存活主机ip。

arpings.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#!/bin/bash
interfaces=($(ifconfig|grep ': '|awk -F ':' '{print $1}'))  # get avaliable interface
if [ $# != 1 ]||[ $1 = '-h' ]||[ $1 = "--help" ]  # no interface selected
then
	echo "Usage: ./arpings interface"
	echo "Avaliable interface:"
	echo "	${interfaces[@]}"
	echo "For example, scanning at eth0 interface: ./arpings eth0"
	exit	
fi

for interface in ${interfaces[@]}
do
	if [ $interface = $1 ]
	then
		ip=$(ifconfig|grep -A 1 $1|awk -F 'inet' '{print $2}'|awk '{print $1}')
		for i in $(seq 1 254)
		do
			if [ $(arping -I $1 -c 1 ${ip%.*}.$i|grep 'Received'|awk '{print $2}') = '1' ]  # output survival host
			then
				echo ${ip%.*}.$i  
			fi
		done
		exit
	fi
done

echo "Wrong interface!"
echo "Avaliable interface:"
echo "	${interfaces[@]}"