Skip to content

Instantly share code, notes, and snippets.

@0x5e
Last active December 15, 2022 01:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 0x5e/ab9c95c64b281de3df421aec5c41722f to your computer and use it in GitHub Desktop.
Save 0x5e/ab9c95c64b281de3df421aec5c41722f to your computer and use it in GitHub Desktop.
Amlogic S9xxx network failsafe script
#!/bin/sh
# ./amlogic-s9xxx-network-failsafe.sh
# 使用方式:在 Amlogic S9xxx 盒子及其 Docker 容器的 /etc/rc.local 中加入此文件,可以在开机后网络异常的情况下自动修复。若修复之后 60s 仍不正常,会自动重启。
interface="eth0"
_logger() {
echo "[`basename "$0"`] $1"
}
is_network_ready() {
if ping -c 1 127.0.0.1 2>&1 | grep "sendto: Operation not permitted" > /dev/null ; then
_logger "Network failure. Operation not permitted."
return 1
fi
if !(ip addr show $interface | grep "state UP" > /dev/null ) ; then
_logger "Network failure. $interface is DOWN."
return 2
fi
ipv4_addr=`ip addr show $interface | awk '/inet / {print $2}'`
if [ -z $ipv4_addr ]; then
_logger "Network failure. IP: null."
return 3
fi
_logger "Network is ready. IP: $ipv4_addr."
return 0
}
if is_network_ready ; then
exit 0
fi
# Fix up network
_logger "Restarting $interface..."
ifconfig $interface down && ifconfig $interface up
# Wait for network ready
n=0
until [ "$n" -ge 20 ]
do
sleep 3
is_network_ready && exit 0
n=$((n+1))
done
_logger "Timeout. Rebooting..."
# TODO: reboot?
reboot
@uniartisan
Copy link

您好 我实在 /etc/init.d 新增了这个文件,但是由于运行的太早网卡初始化等问题还不能达到目的。在开头增加 sleep 10s后完美解决。

@0x5e
Copy link
Author

0x5e commented Sep 11, 2022

@uniartisan 我是放在rc.local中的,缺点就是一旦网络异常了,启动时间会比较长一些。放在init.d是希望更早的修复吗还是出于什么目的?

@uniartisan
Copy link

@uniartisan 我是放在rc.local中的,缺点就是一旦网络异常了,启动时间会比较长一些。放在init.d是希望更早的修复吗还是出于什么目的?

基于ubuntu的版本没有rc.local文件夹,只有rc.local脚本文件,所以我就放在init.d文件夹了 :)

@0x5e
Copy link
Author

0x5e commented Sep 12, 2022

@uniartisan 我的原意其实是脚本放在何处都行,最后在/etc/rc.local文件里加一行执行一下就好了:)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment