shell脚本应用实战


 这个脚本主要记录局域网中各主机的MAC地址
(每行的第一条记录,第一列为IP地址,第二行为对应的MAC地址)

    通过arping命令发送arp请求,根据反馈结果记录MAC地址

例如192.168.10网段    (arping-c2-w1192.168.10.1)
查看相应的IP地址对应的MAC地址
arp-n|grep-v'Address'|awk'{print$1,$3}'
    将文件保存到/etc/ether文件中
   
vimgetarp.sh

#!/bin/bash

foriin$(seq120);do
echo192.168.6.$i
arping-c1-w1192.168.6.$i&>/dev/null
done

if[-f/etc/ether];then
mv/etc/ether/etc/ether.bak
fi

:wq

arp-n|grep-v'Address'|awk'{print$1,$3}'>/etc/ether

bashgetarp.sh(执行一下脚本)
cat/etc/ether(查看结果)


扫描主机是否开启22端口

vimscanhost.sh

#!/bin/bash


foriin$(awk'{print$1}'/etc/ether);do
#echowgetftp://$i

nc-z$i22

if[$?-eq0];then
echoHost$isshisup
else
echoHost$isshisdown
fi

done

bashscanhost.sh执行