循环ping两个地址测试服务器连通性
方法一:
#!/bin/bash
M=/tmp/jiankong/`date '+%Y-%m-%d'`
if [ ! -d $M ];then
mkdir -p $M
echo -e "\033[32mThe $M Create Successfully...\033[0m"
else
echo -e "\033[32mThis $M is exists...\033[0m"
fi
interval=1
while true; do
ip1="127.0.0.1"
ip2="10.0.0.200"
result=$(ping -c 4 $ip1)
if [ $? -eq 0 ]; then
echo "`date '+%Y-%m-%d %H:%M:%S'` $ip1 is up!" >> "$M/up.txt"
else
echo "`date '+%Y-%m-%d %H:%M:%S'` $ip1 is down!" >> "$M/down.txt"
fi
result=$(ping -c 4 $ip2)
if [ $? -eq 0 ]; then
echo "`date '+%Y-%m-%d %H:%M:%S'` $ip2 is up!" >> "$M/up.txt"
else
echo "`date '+%Y-%m-%d %H:%M:%S'` $ip2 is down!" >> "$M/down.txt"
fi
sleep $interval
done
方法二:
#!/bin/bash
while true; do
ip1="目标IP1" # 将 "目标IP1" 替换为第一个要Ping的IP地址
ip2="目标IP2" # 将 "目标IP2" 替换为第二个要Ping的IP地址
result=$(ping -c 4 $ip1) # 发送四次ICMP Echo Request到第一个IP地址并获取结果
echo "$result" | grep '0% packet loss' > /dev/null && echo "$ip1 is reachable." || echo "$ip1 is not reachable."
result=$(ping -c 4 $ip2) # 发送四次ICMP Echo Request到第二个IP地址并获取结果
echo "$result" | grep '0% packet loss' > /dev/null && echo "$ip2 is reachable." || echo "$ip2 is not reachable."
done