我有一个包含很多ips的文件,我想循环它们,平它们,并将在线的文件打印到另一个文件中。我知道循环文件,平他们,但我不知道如何读取输出,以知道ip是否在线。
发布于 2015-12-31 11:25:14
使用此脚本
1. File full of IP's
[root@localhost scripts]# cat iplist.txt
172.31.57.63
localhost
127.0.0.1
172.31.57.62
2. Create the script
[root@localhost scripts]# cat pingips.sh
#!/bin/bash
up_ipfile='online_server.txt'
while IFS= read -r ips; do
ping -c 1 $ips > /dev/null 2>&1
if [ $? -eq 0 ]; then
echo $ips >> $up_ipfile
fi
done < iplist.txt
3. Run the script after making it executable
[root@localhost scripts]# ./pingips.sh
4. It will create a file with IP's which are alive
[root@localhost scripts]# cat online_server.txt
172.31.57.63
localhost
127.0.0.1
发布于 2015-12-31 10:57:49
检查ping命令的退出状态。if ping ...; then echo online; fi
请注意,您必须使用-c
和-w
标志限制ping时间。
https://stackoverflow.com/questions/34545284
复制相似问题