#!/bin/bash
PppCheck() {
ppp_interfaces=$(ip r | grep -o "\<ppp[0-9]\+\>" | awk -F'ppp' '{print $2}' | sort -n |uniq | awk 'BEGIN{ORS=" "}{print "ppp"$NF}')
for iface in $ppp_interfaces; do
if ping -I $iface -W 5 -c 2 223.5.5.5 >/dev/null 2>&1; then
dial_ip=$(ip route show dev $iface | awk '{print $1}')
echo "设置默认网关为 $dial_ip (通过 $iface)"
# 检查是否存在默认路由
if ip route | grep -q '^default'; then
ip route replace default via $dial_ip dev $iface
else
ip route add default via $dial_ip dev $iface
fi
if ping -W 5 -c 2 223.5.5.5 >/dev/null 2>&1; then
echo "网络正常,修改默认网关成功"
return 0
else
echo "网络不通,修改默认网关失败"
fi
fi
done
echo "网络不通,修改默认网关失败"
return 1
}
check_ping() {
if ping -W 5 -c 2 223.5.5.5 >/dev/null 2>&1; then
echo "网络正常,不修改默认网关"
return 0
else
echo "网络不通,修改默认网关"
return 1
fi
}
if check_ping; then
exit 0
fi
mgmt_iface="$(cat /root/iface.txt 2>/dev/null)"
if [ -n "$mgmt_iface" ]; then
if ip link show "$mgmt_iface" | grep -q "state UP"; then
mgmt_ip=$(ip route show dev "$mgmt_iface" | awk '/^default/{print $3}')
if [ -n "$mgmt_ip" ]; then
echo "设置默认网关为 $mgmt_ip (通过 $mgmt_iface)"
# 检查是否存在默认路由
if ip route | grep -q '^default'; then
ip route replace default via "$mgmt_ip" dev "$mgmt_iface"
else
ip route add default via "$mgmt_ip" dev "$mgmt_iface"
fi
fi
if check_ping; then
exit 0
fi
else
echo "$mgmt_iface 接口未开启"
fi
else
echo "/root/iface.txt文件为空"
fi
PppCheck
标签:iface,--,ip,拨号,dev,echo,ppp,mgmt,fi
From: https://blog.51cto.com/hequan/6161840