osnosn

  博客园 :: 首页 :: 博问 :: 闪存 :: :: 联系 :: 订阅 订阅 :: 管理 ::

OpenWRT19.07_命令行_重拨wan_重启路由_wget/curl

转载注明来源: 本文链接 来自osnosn的博客,写于 2020-10-19.

写OpenWRT的脚本时,需要用到一些重启命令

以下的命令中的参数"wan",是基于wan口的名称为"WAN"
ssh 登录到openwrt上。 执行以下命令:

  • 重启路由 /sbin/reboot , ssh 连接立刻就会断开。
  • 重启所有网络 /etc/init.d/network restart , 这个命令会导致 ssh 连接卡住(约卡3-5秒),等到网络重启之后,ssh才会恢复。本机执行没有问题。
    • 因为 network restart 先会 ifdown -a 把所有网络/网卡都停掉,再全部网卡重新启动。
  • 重拨wan /sbin/ifup wan , 等于ifdown wan && ifup wan。这个命令执行很快,ssh连接不卡,且立即就返回。wan口大约10秒后就换了个IP,对应的wan6也会换IP。
    • 这个 ifup 命令,最终是通过 ubus call network.interface down '{"interface":"wan"}' + ubus call network.interface up '{"interface":"wan"}' 执行的。
    • 显式的执行 /sbin/ifdown wan && /sbin/ifup wan 也是没有问题的。
    • 实际使用中,仅执行ifup wan,有时发现ipv4不会变,仅ipv6变。所以为了更换IP,ifdown wan;sleep 2;ifup wan更好。
  • 在web界面Interfaces页面,点击WAN对应的 "Restart" 按键。其实执行的就是 /sbin/ifup wan
  • 在web界面Interfaces页面,点击WAN对应的 "Stop" 按键。其实执行的就是 /sbin/ifdown wan
  • 重启wifi /sbin/wifi down && /sbin/wifi up

实际使用

  • 在Linux中创建一个 key, 用 ssh-keygen -t rsa -b 1024 -f opwrt , 然后把 opwrt.pub 中的内容 copy 到 路由器的 /etc/dropbear/authorized_keys 文件中。
    • shell脚本,重启路由器就执行 ssh -i opwrt root@192.168.1.1 "/sbin/reboot"
    • shell脚本,重拨wan口就执行 ssh -i opwrt root@192.168.1.1 "/sbin/ifup wan"
    • python3脚本,import subprocess 然后 subprocess.getoutput('ssh -i opwrt root@192.168.1.1 "/sbin/ifup wan" ')
    • 另: 以上的 ssh 可以再带上一个参数 ssh -o "ConnectTimeout=5"
  • 在路由器中,
    • 写shell脚本,直接就执行 reboot 或者ifup wan
    • lua 脚本,首先 require("luci.sys") , 然后执行 luci.sys.exec("/sbin/ifup wan")
  • windows 的 bat 批处理
    • 可以考虑用 putty的命令行版 plink.exe 去ssh登录执行命令。(可以用 puttygen.exe 去生成key,放到authorized_keys中 )

其他1

  • 如果频繁掉线,可以考虑修改这两个配置项。(修改方法,自行搜索)
    • network -> interface -> WAN (pppoe) -> EDIT -> Advanced Settings -> LCP echo failure thresholdLCP echo interval
      网络 -> 接口 -> WAN (pppoe) -> 编辑 -> 高级设置 -> LCP 响应故障阀值LCP 响应间隔
    • /etc/ppp/option 中的 lcp-echo-failurelcp-echo-interval
  • 有时会出现(偶尔),wan连接正常,但无法访问网络。可以考虑写个脚本,定时执行。
    • 思路是:
      先访问一下百度,看是否正常,再访问一下搜狗,再访问一下163。如果三个站都无法访问,就执行 ifup wan 重拨。
      测试访问几个站,自己决定,测试哪几个站,自己挑选。用shell编程即可,装个完整版wget,用wget访问,判断返回值,就知道访问是否成功。
      记得设置timeout参数,wget的默认timeout好像是60秒,太长了。
      还要设置重试次数大于2,防止第一次访问因dns查询返回太慢而失败。
    • 以下给出一个例子参考,需要opkg install wget ca-bundleopkg install curl 支持。
      对于op-21, op-22 用 opkg install wget-sslopkg install curl
      还需要 opkg install bind-dig
      然后放入 crontab 中定时执行 */10 * * * * /bin/sh /root/chk_net_redail.sh
#!/bin/sh
# filename: /root/chk_net_redail.sh
# 4=network error/refused/timeout/dns err/  #wget的错误码
# 6=resolve host failed, 7=refused, 28=timeout, 60=ssl not trust  #curl的错误码
# need "opkg install wget ca-bundle" or "opkg install wget-ssl" or "opkg install curl"

count=0
URLs='https://baidu.com  http://163.com  http://www.qq.com  http://sogou.com'
for host in $URLs ; do
    #echo $host
    domain=${host#*://}
    domain=${domain%%/*}
    domain=${domain%:*}
    myip=''
    if echo "$domain"|[ -z "$(sed -n '/^[0-9][.0-9]*$/p')" ]; then  #非IP
        myip=$(dig $domain a +timeout=3 +tries=1 +short)  #获取域名解析,需要"opkg install bind-dig"支持
    fi
    # wget的-t 这个参数要>=2, curl的--retry要>=1, 防止第一次访问dns解析超时,而导致失败
    #stderr=$(wget -nv -T3 -t3 --no-proxy --retry-on-host-error --method=HEAD --max-redirect=0 "$host" 2>&1 )
    stderr=$(curl -SsfI -x "" -m3 --max-redirs 1 --retry 2 --retry-delay 2 --no-progress-meter "$host" 2>&1 > /dev/null )
    retcode=$?
    if [ -n "${stderr}" ]; then  #记录一下,前面加上时间戳
        echo $(/bin/date '+%F_%T%z') \"$host\" \"$myip\" "${stderr}" >> /root/log.redail
    fi
    #if [ 4 -eq $retcode ]; then  #如果上面用 wget
    if [ 6 -eq ${retcode} -o 7 -eq ${retcode} -o 28 -eq ${retcode} ]; then #如果上面用 curl
        echo $(date +%F_%T%z) \"$host\" ${retcode} 'error.' >> /root/log.redail
        count=$((count+1))
    else
        break
    fi
done
URL_cnt=$(echo $URLs|wc -w)       #测试几个网站
if [ $count -ge $URL_cnt ]; then  #是不是所有的都失败
    echo $(date +%F_%T%z) redail >> /root/log.redail
    ifdown wan; sleep 1; ifup wan
fi

其他2


转载注明来源: 本文链接 https://www.cnblogs.com/osnosn/p/13842055.html 来自osnosn的博客.

posted on 2020-10-19 19:40  osnosn  阅读(10875)  评论(0编辑  收藏  举报