网关1为192.168.0.1,网关2为172.16.111.1,编写shell脚本,实现网络切换
时间: 2024-05-19 07:16:07 浏览: 116
192.168.1.253手机上打不开的解决办法
#!/bin/bash
# 判断当前默认网关是哪一个
default_gateway=$(route -n | awk '{if($1=="0.0.0.0") print $2}')
if [ "$default_gateway" = "192.168.0.1" ]; then
# 当前默认网关为网关1,切换到网关2
echo "Switching to Gateway 2"
route del default gw 192.168.0.1
route add default gw 172.16.111.1
elif [ "$default_gateway" = "172.16.111.1" ]; then
# 当前默认网关为网关2,切换到网关1
echo "Switching to Gateway 1"
route del default gw 172.16.111.1
route add default gw 192.168.0.1
else
# 默认网关不是网关1也不是网关2
echo "Unknown default gateway: $default_gateway"
fi
# 输出新的默认网关
echo "New default gateway: $(route -n | awk '{if($1=="0.0.0.0") print $2}')"
阅读全文