需求描述:
通过给脚本传出一个包含所有ip的文本文件,然后实现对文件内部的ip进行批量判断联通性
实现代码:
#!/bin/bash
#ping check
#v1.4 by chen 2020年11月16日
>up.txt
>down.txt
if [ $# -eq 0 ];then
echo "usage:`basename $0` file"
exit
fi
for i in `cat $1`
do
{
ping -c1 -W1 $i &>/dev/null
if [ $? -eq 0 ];then
echo -e "\033[32m$i is up\033[0m" | tee -a up.txt
else
echo -e "\033[31m$i is down\033[0m" | tee -a down.txt
fi
}&
done
wait
echo "finishi.."
测试结果:
root@cyt:/home/shell/ping/ping02# ls
2.txt down.txt ip.txt ping02.sh ping03.sh ping04.sh
root@cyt:/home/shell/ping/ping02# cat 2.txt
192.168.100.154
192.168.100.246
192.168.100.188
192.168.100.1
192.168.100.100
root@cyt:/home/shell/ping/ping02# bash ping04.sh 2.txt
192.168.100.246 is up
192.168.100.188 is up
192.168.100.1 is up
192.168.100.154 is up
192.168.100.100 is down
finishi…
root@cyt:/home/shell/ping/ping02# cat up.txt
192.168.100.246 is up
192.168.100.188 is up
192.168.100.1 is up
192.168.100.154 is up
root@cyt:/homeshell/ping/ping02# cat down.txt
192.168.100.100 is down
root@cyt:/home/shell/ping/ping02#