【网络配置】Bash基于Ping自动切换路由

微信扫一扫,分享到朋友圈

【网络配置】Bash基于Ping自动切换路由
0

自动切换下一条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
咱很菜的,是个智障。 人设非常容易崩。
上一篇

【创意咖啡】创意咖啡的原料知识-基础

下一篇

【人生感悟?生活随想?牢骚?】以前、现在、未来

你也可能喜欢

发表评论

您的电子邮件地址不会被公开。 必填项已用 * 标注

提示:点击验证后方可评论!

插入图片

排行榜

    抱歉,30天内未发布文章!
返回顶部