找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
广告投放联系QQ68610888
查看: 2607|回复: 6

转一个断网重启脚本

[复制链接]
发表于 2021-11-17 07:27 | 显示全部楼层 |阅读模式
    用一个orangepi总是断网,终网上找了很多,总不能如愿,终google了一个,分享一下:

#!/bin/sh

#=====================================================================
# pingtest.sh, v1.0.1
# Created 2009 by Bennett Lee
# Released to public domain
#
# (1) Attempts to ping several hosts to test connectivity.  After
#     first successful ping, script exits.
# (2) If all pings fail, resets interface and retries all pings.
# (3) If all pings fail again after reset, then reboots pfSense.
#
# History
# 1.0.1   Added delay to ensure interface resets (thx ktims).
# 1.0.0   Initial release.
#=====================================================================

#=====================================================================
# USER SETTINGS
#
# Set multiple ping targets separated by space.  Include numeric IPs
# (e.g., remote office, ISP gateway, etc.) for DNS issues which
# reboot will not correct.
ALLDEST="baidu.com sohu.com 223.5.5.5 223.6.6.6"
# Interface to reset, usually your WAN
BOUNCE=em0
# Log file
LOGFILE=/root/pingtest.log
#=====================================================================

COUNT=1
while [ $COUNT -le 2 ]
do

        for DEST in $ALLDEST
        do
                #echo `date +%Y%m%d.%H%M%S` "Pinging $DEST" >> $LOGFILE
                ping -c1 $DEST >/dev/null 2>/dev/null
                if [ $? -eq 0 ]
                then
                        #echo `date +%Y-%m-%d.%H:%M:%S` "Ping $DEST OK." >> $LOGFILE
                        exit 0
                fi
        done

        if [ $COUNT -le 1 ]
        then
                echo `date +%Y-%m-%d.%H:%M:%S` "All pings failed. Resetting interface $BOUNCE." >> $LOGFILE
                /sbin/ifconfig $BOUNCE down
                # Give interface time to reset before bringing back up
                sleep 10
                /sbin/ifconfig $BOUNCE up
                # Give WAN time to establish connection
                sleep 60
        else
                echo `date +%Y-%m-%d.%H:%M:%S` "All pings failed twice. Rebooting..." >> $LOGFILE
                /sbin/shutdown -r now >> $LOGFILE
                exit 1
        fi

        COUNT=`expr $COUNT + 1`
done



结果如下:
2021-11-16.14:00:02 Ping baidu.com OK.
2021-11-16.16:00:02 Ping baidu.com OK.
2021-11-16.18:00:02 Ping baidu.com OK.
2021-11-16.20:00:02 Ping baidu.com OK.
2021-11-16.22:00:02 Ping baidu.com OK.
2021-11-17.00:00:02 Ping baidu.com OK.
2021-11-17.02:00:02 Ping baidu.com OK.
2021-11-17.04:02:14 All pings failed. Resetting interface em0.
2021-11-17.04:05:36 All pings failed twice. Rebooting...
2021-11-17.06:00:02 Ping baidu.com OK.



我的恩山、我的无线 The best wifi forum is right here.
发表于 2021-11-17 11:10 | 显示全部楼层
谢谢分享谢谢
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

 楼主| 发表于 2021-11-18 18:22 | 显示全部楼层
# Interface to reset, usually your WAN
BOUNCE=em0


初测试没发现,对于armbian,em0应该是eth0
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

发表于 2021-11-22 15:24 | 显示全部楼层
好!啊啊啊啊啊啊啊啊啊啊啊啊啊
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

发表于 2021-11-22 15:24 | 显示全部楼层
好!啊啊啊啊啊啊啊啊啊啊啊啊啊
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

发表于 2021-11-28 09:19 | 显示全部楼层
收藏加复制
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

 楼主| 发表于 2021-12-10 04:19 | 显示全部楼层
不得不说,我的这个orange pi one的网口破到了一定的地步,安装以后有这么多重启,可慰的是,脚本是有效的。

root@orangepione:~# cat pingtest.log
2021-11-28.03:07:13 All pings failed. Resetting interface eth0.
2021-11-28.03:10:35 All pings failed twice. Rebooting...
2021-11-28.03:11:16 All pings failed. Resetting interface eth0.
2021-11-28.03:12:26 All pings failed twice. Rebooting...
2021-12-04.11:52:13 All pings failed. Resetting interface eth0.
2021-12-04.11:55:36 All pings failed twice. Rebooting...
2021-12-04.11:56:05 All pings failed. Resetting interface eth0.
2021-12-04.11:57:16 All pings failed twice. Rebooting...
2021-12-05.03:07:13 All pings failed. Resetting interface eth0.
2021-12-05.03:10:36 All pings failed twice. Rebooting...
2021-12-05.03:11:16 All pings failed. Resetting interface eth0.
2021-12-05.03:12:27 All pings failed twice. Rebooting...
2021-12-06.13:47:13 All pings failed. Resetting interface eth0.
2021-12-06.13:50:36 All pings failed twice. Rebooting...
2021-12-06.13:52:03 All pings failed. Resetting interface eth0.
2021-12-06.13:53:14 All pings failed twice. Rebooting...
2021-12-08.03:07:14 All pings failed. Resetting interface eth0.
2021-12-08.03:10:36 All pings failed twice. Rebooting...
2021-12-08.03:11:06 All pings failed. Resetting interface eth0.
2021-12-10.00:37:14 All pings failed. Resetting interface eth0.
2021-12-10.00:40:36 All pings failed twice. Rebooting...
2021-12-10.00:40:51 All pings failed. Resetting interface eth0.
2021-12-10.00:42:01 All pings failed twice. Rebooting...
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

有疑问请添加管理员QQ86788181|手机版|小黑屋|Archiver|恩山无线论坛(常州市恩山计算机开发有限公司版权所有) ( 苏ICP备05084872号 )

GMT+8, 2024-3-29 01:03

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

| 江苏省互联网有害信息举报中心 举报信箱:js12377 | @jischina.com.cn 举报电话:025-88802724 本站不良内容举报信箱:68610888@qq.com 举报电话:0519-86695797

快速回复 返回顶部 返回列表