Last active
December 15, 2022 01:56
-
-
Save 0x5e/ab9c95c64b281de3df421aec5c41722f to your computer and use it in GitHub Desktop.
Amlogic S9xxx network failsafe script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
基于ubuntu的版本没有rc.local文件夹,只有rc.local脚本文件,所以我就放在init.d文件夹了 :)