批量pingIP脚本

linux和windows批量ping脚本


linux下批量ping的shell脚本


方法一
将ip地址写入ip.txt
[root@localhost ~]# cat ping.sh
#!/bin/bash
cat ip.txt |while read line;
do
ip=echo $line |awk ‘{print $1}’
ping -c 3 -w 2 $ip &> /dev/null
if [ $? -eq 0 ];then
echo $ip live
else
echo $ip dead
fi
done

方法二
[root@localhost ~]# cat ping2.sh
#!/bin/bash
cat ip.txt |while read line;
do
ip=echo $line |awk ‘{print $1}’
ping -c 2 $ip &> /dev/null && echo $ip up ||echo $ip down
done
————————————————

windows系统用的批量ping的bat脚本


首先需要将你要ping的IP地址保存到一个ip.txt文本文件中。

ip.txt

192.168.0.1

192.168.0.2

192.168.0.3

然后在DOS窗口下输入如下命令即可在当前目前中创建ok.txt和no.txt两个文本文件

for /f %i in (ip.txt) do (ping %i -n 1 && echo %i>>ok.txt || echo %i >>no.txt)

如果要另存为bat文件,书写格式如下:

for /f %%i in (ip.txt) do (ping %%i -n 1 && echo %%i>>ok.txt || echo %%i >>no.txt)

另将ping到的结果只保存到一个文件的写法:

for /f %%i in (ip.txt) do (ping %%i -n 1 >>ip-info.txt)

你可能感兴趣的:(shell脚本,小脚本,个人心得,批量pingIP)