编写一个getarp.sh的脚本,记录局域网内各主机的MAC地址。 保存到/etc/ethers文件中,若此文件已存在,则先转移进行备份。 每行一条记录,第1列为ip地址,第2列为对应的MAC地址。 编写一个scanhost.sh的扫描脚本,检查有哪些主机开启了ftp服务,扫描对象是/etc/ethers中所有的ip地址。 如下:
[root@localhost ~]# vim getarp.sh
#!/bin/bash
NADD="192.168.1." #需要扫描的网段
FILE="/etc/ethers"
[ -f $FILE ] && /bin/cp -f $FILE $FILE.old # -f为强制复制
HADD=1 #起始扫描地址
while [ $HADD -lt 10 ] #最大扫描主机地址为10
do
ping -c 2 -i 0.2 -W 3 ${NADD}${HADD} &> /dev/null
if [ $? -eq 0 ] ; then
arp -n | grep ${NADD}${HADD} | awk '{print $1,$3}' >> $FILE
fi
let HADD++
done
#保存退出,进行扫描:
[root@localhost ~]# . getarp.sh #扫描的时间长短,与定义的扫描范围有关。
[root@localhost ~]# cat /etc/ethers #查看扫描结果
192.168.1.2 00:0c:29:9a:09:98
192.168.1.8 00:50:56:c0:00:01
扫描端口,检查哪些主机开启了匿名ftp服务:
[root@localhost ~]# vim scanhost.sh
#!/bin/bash
TARGET=$(awk '{print $1}' /etc/ethers)
echo "以下主机已开放匿名FTP服务:"
for IP in $TARGET
do
wget ftp://$IP/ &> /dev/null
if [ $? -eq 0 ] ; then
echo $IP
rm -rf index.html
fi
done
#保存退出,进行扫描:
[root@localhost ~]# . scanhost.sh
以下主机已开放匿名FTP服务:
192.168.1.2