-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpadavan_auto_gateway.sh
45 lines (40 loc) · 1.25 KB
/
padavan_auto_gateway.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/bin/sh
#本脚本用于padavan主路由,通过判断旁路由是否在线,来更新网关
#遗憾的是dhcp没找到强制更新,更新网关还需要设备重新联网
#用参考了别人的脚本,但是那个需要手动修改dnsmaqs.conf,这里用padavan自身的配置命令nvram和rc事件来更新,更简洁
###然后放到crontab里,每分钟检测一次
# * * * * * /etc/storage/bin/auto_gateway.sh
bypass=192.168.123.2
default_ip=192.168.123.1
network()
{
#通过判断路由器的页面能否打开判断旁路由在不在线
local timeout=2
local ret_code=`curl -I -s -m ${timeout} ${bypass} -w %{http_code} | tail -n1`
if [ "x$ret_code" = "x200" ]; then
return 1
else
return 0
fi
}
changeGateway()
{
local current_gateway=`nvram get dhcp_gateway_x` #获取当前的网关配置
if [ $current_gateway = $1 ];then #如果相等就不用改了,
# /usr/bin/logger "[bypass detect] gateway don't need to change"
return 0
else
nvram set dhcp_gateway_x=$1 #修改网关和dns,然后重启dhcpd服务
nvram set dhcp_dns1_x=$1
nvram commit
restart_dhcpd
/usr/bin/logger "[bypass detect] change gateway to $1 success"
return 0
fi
}
network
if [ $? -eq 0 ];then
changeGateway $default_ip
else
changeGateway $bypass
fi