自动切换下一条bash,基于ping
#!/bin/bash
primary_nexthop="192.168.50.1" # 定义主nexthop IP地址
backup_nexthop="192.168.52.254" # 定义备用nexthop IP地址
ip_subnet="1.14.5.0/24" # 定义要切换到的IP段
ip_to_test="1.14.5.14" # 定义要测试的IP地址
ping -c 3 $ip_to_test > /dev/null 2>&1 # 发送ping命令以测试IP可达性
if [ $? -ne 0 ] # 如果ping命令返回非零退出码,则说明IP不可达
then
current_nexthop="$(ip route show | grep $ip_subnet | awk '{print $3}')" # 获取当前的目标IP段路由
if [ "$current_nexthop" == "$primary_nexthop" ] # 如果当前的路由是主nexthop
then
echo "IP address $ip_to_test is unreachable. Adding route to $ip_subnet via backup nexthop $backup_nexthop..."
ip route add $ip_subnet via $backup_nexthop # 添加目标IP段的路由
else
echo "IP address $ip_to_test is unreachable, but using backup nexthop $backup_nexthop..."
fi
else
current_nexthop="$(ip route show | grep $ip_subnet | awk '{print $3}')" # 获取当前的目标IP段路由
if [ "$current_nexthop" == "$backup_nexthop" ] # 如果当前的路由是备用nexthop
then
echo "IP address $ip_to_test is reachable. Switching back to primary nexthop $primary_nexthop..."
ip route del $ip_subnet # 删除当前的路由
ip route add $ip_subnet via $primary_nexthop # 添加主nexthop作为路由
else
echo "IP address $ip_to_test is reachable, using primary nexthop $primary_nexthop."
fi
if [ "$current_nexthop" == "" ] # 如果当前不存在路由
then
echo "IP address $ip_to_test is reachable. Adding route to $ip_subnet via primary nexthop $primary_nexthop..."
ip route add $ip_subnet via $primary_nexthop # 添加主nexthop作为路由
fi
fi
ipv4优先 debian
sed -i 's/#precedence ::ffff:0:0\/96 100/precedence ::ffff:0:0\/96 100/' /etc/gai.conf
iptables端口转发
iptables -t nat -A PREROUTING -p tcp -i eth0 --dport 1000:3000 -j DNAT --to 192.168.0.211:1521
iptables -t nat -A POSTROUTING -j MASQUERADE