脚本mac.sh代码如下
example: 192.168.1.0/24 扫描这个网络段的在线IP
for ip in {1..254}
do
sh mac.sh 192.168.1.$a
done

#!/bin/sh
if [ -z $1 ];then
echo "Please input ipaddress"
exit
fi
ip=$1
filestat=$(ping -c1 $ip |grep transmitted |awk '{print $4}')
if [ $filestat -eq 0 ];then
echo "$ip is not online"
exit
fi
ping $ip -c1 > /dev/null
mac=`arp $ip |awk '{print $3}'|grep ":"`
name=$(/usr/bin/nbtscan $ip  |awk '{print $2}' |cut -d "\\" -f2)
echo "Address:$ip,HWaddress:$mac,Hostname:$name"
mysql -uroot -p123456 mac -e  "insert into mac_addr_hostname(ip,mac,hostname) values ('$ip','$mac','$name')


创建数据库,以下是表结构:

-- Table "mac_addr_hostname" DDL
CREATE TABLE `mac_addr_hostname` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(32) DEFAULT NULL,
  `ip` varchar(64) DEFAULT NULL,
  `mac` varchar(64) DEFAULT NULL,
  `hostname` varchar(32) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=69 DEFAULT CHARSET=utf8;

可以用于统计内网用户lol.gif

tar zxvf shell.tar.gz
cp nbtscan /usr/bin/
chmod +x /usr/bin/nbtscan


原文已经发在:Linuxtone