扫描局域网并取得ip/mac对
扫描局域网并取得ip/mac对
生成一个文本, 这个文本可做为“受限局域网中借用他人ip和mac上网”一文中的DB
详见 http://www.linuxeden.com/forum/thread-161488-1-1.html
复制内容到剪贴板
代码:
#! /bin/bash
## file: get_ipmac.sh
## scan ip mac
## [email]huanlf@gmail.com[/email]
IPMAC_FILE='/home/huan/mydoc/ipmac'
DEFAULT_LAN="192.168.1.0/24"
BLACK_IP="192.168.1.1"
LAN="$1"
[[ -z "$LAN" ]] && LAN="$DEFAULT_LAN"
if (( UID != 0 )); then
echo "root privilege to get mac"
exit 1
fi
## just scan ...
scan_lan()
{
nmap -n -sP "$LAN"
}
## get ip mac
get_ipmac()
{
awk '{
if ( $1 ~ /^Host/ ) {
ip = $2;
}
else if ( $1 ~ /^MAC/ ) {
mac = $3;
print ip, mac;
}
}'
}
## check if the ip in blacklist
is_inblack()
{
local ip=$1
for black_ip in $BLACK_IP; do
[[ "$black_ip" == "$ip" ]] && return 0
done
return 1
}
## check if the ip in db file
is_indb()
{
local ipmac="$1 $2"
grep -xq "$ipmac" "$IPMAC_FILE"
return $?
}
## update db file
update_db()
{
local ip
local mac
while read ip mac; do
if is_inblack "$ip" || is_indb "$ip" "$mac"; then
:
else
echo "$ip $mac" | tee -a "$IPMAC_FILE"
fi
done
}
#-------------------- main --------------------
scan_lan |
get_ipmac |
update_db