Answers:
本质上,这是沃里克的答案,只需分步说明即可。
在您的主文件夹中创建以下shell脚本:
check_inet.sh
#!/bin/bash
TMP_FILE=/tmp/inet_up
# Edit this function if you want to do something besides reboot
no_inet_action() {
shutdown -r +1 'No internet.'
}
if ping -c5 google.com; then
echo 1 > $TMP_FILE
else
[[ `cat $TMP_FILE` == 0 ]] && no_inet_action || echo 0 > $TMP_FILE
fi
更改权限,使其可执行
$ chmod +x check_inet.sh
/etc/crontab
使用编辑sudo
并添加以下行(替换yourname
为您的实际用户名):
*/30 * * * * /home/yourname/check_inet.sh
我认为hololeap解决方案正在工作。
我的解决方案每隔N分钟检查一次(取决于您如何配置crontab)网络连接是否正常。如果检查失败,我会跟踪失败情况。当故障计数大于5时,我尝试重新启动wifi(如果wifi重新启动失败,您也可以重新启动Raspberry,请检查注释)。
这是一个GitHub存储库,其中始终包含脚本的最新版本:https : //github.com/ltpitt/bash-network-repair-automation
根据stackexchange的一般政策(所有答案都不应仅包含链接),在这里也是文件network_check.sh,将其复制并粘贴到您喜欢的任何文件夹中,安装说明在脚本注释中。
#!/bin/bash
# Author:
# twitter.com/pitto
#
# HOW TO INSTALL:
#
# 1) Install ifupdown and fping with the following command:
# sudo apt-get install ifupdown fping
#
# 2) Then install this script into a folder and add to your crontab -e this row:
# */5 * * * * /yourhome/yourname/network_check.sh
#
# Note:
# If you want to perform automatic repair fsck at reboot
# remember to uncomment fsck autorepair here: nano /etc/default/rcS
# Let's clear the screen
clear
# Write here the gateway you want to check to declare network working or not
gateway_ip='www.google.com'
# Here we initialize the check counter to zero
network_check_tries=0
# Here we specify the maximum number of failed checks
network_check_threshold=5
# This function will be called when network_check_tries is equal or greather than network_check_threshold
function restart_wlan0 {
# If network test failed more than $network_check_threshold
echo "Network was not working for the previous $network_check_tries checks."
# We restart wlan0
echo "Restarting wlan0"
/sbin/ifdown 'wlan0'
sleep 5
/sbin/ifup --force 'wlan0'
sleep 60
# If network is still down after recovery and you want to force a reboot simply uncomment following 4 rows
#host_status=$(fping $gateway_ip)
#if [[ $host_status != *"alive"* ]]; then
# reboot
#fi
}
# This loop will run network_check_tries times and if we have network_check_threshold failures
# we declare network as not working and we restart wlan0
while [ $network_check_tries -lt $network_check_threshold ]; do
# We check if ping to gateway is working and perform the ok / ko actions
host_status=$(fping $gateway_ip)
# Increase network_check_tries by 1 unit
network_check_tries=$[$network_check_tries+1]
# If network is working
if [[ $host_status == *"alive"* ]]; then
# We print positive feedback and quit
echo "Network is working correctly" && exit 0
else
# If network is down print negative feedback and continue
echo "Network is down, failed check number $network_check_tries of $network_check_threshold"
fi
# If we hit the threshold we restart wlan0
if [ $network_check_tries -ge $network_check_threshold ]; then
restart_wlan0
fi
# Let's wait a bit between every check
sleep 5 # Increase this value if you prefer longer time delta between checks
done
编辑2018年1月6日:我删除了临时文件,以使脚本在内存中运行并避免在Raspberry的SD卡上写入。
ifdown
和ifup
,可能会修复网络,可能不会。…………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………… …(续)
我将Pitto 的脚本修改为我的多功能mtac loraWAN网关(无fping)。我还添加了一个日志文件。
#!/bin/bash
# Author:
# twitter.com/pitto
#
# HOW TO INSTALL:
#
# 1) Install ifupdown with the following command:
# sudo apt-get install ifupdown
#
# 2) Create files in any folder you like (ensure that the filename variables, set below,
# match the names of the files you created) with the following commands:
# sudo touch /home/root/scripts/network_check_tries.txt &&
# sudo chmod 777 /home/root/network_check_tries.txt
# sudo touch /home/root/scripts/N_reboots_file.txt &&
# sudo chmod 777 /home/root/N_reboots_file.txt
# sudo touch /home/root/scripts/network_check.log &&
# sudo chmod 777 /home/root/network_check.log
#
# 3) Then install this script into a folder and add to your crontab -e this row:
# */5 * * * * /yourhome/yourname/network_check.sh
#
# Note:
# If additionally you want to perform automatic repair fsck at reboot
# remember to uncomment fsck autorepair here: nano /etc/default/rcS
# Specify the paths of the text file where the network failures count, reboot count,
# and log will be held:
network_check_tries_file='/home/root/network_check_tries.txt'
N_reboots_file='/home/root/N_reboots_file.txt'
log_file='/home/root/network_check.log'
# Save file contents into corresponding variables:
network_check_tries=$(cat "$network_check_tries_file")
N_reboots=$(cat "$N_reboots_file")
# If host is / is not alive we perform the ok / ko actions that simply involve
# increasing or resetting the failure counter
ping -c1 google.com
if [ $? -eq 0 ]
then
# if you want to log when there is no problem also,
# uncomment the following line, starting at "date".
echo 0 > "$network_check_tries_file" #&& date >> "$log_file" && echo -e "-- Network is working correctly -- \n" >> "$log_file"
else
date >> "$log_file" && echo -e "-- Network is down... -- \n" >> "$log_file" && echo "$(($network_check_tries + 1))" > "$network_check_tries_file"
fi
# If network test failed more than 5 times (you can change this value to whatever you
# prefer)
if [ "$network_check_tries" -gt 5 ]
then
# Time to restart ppp0
date >> "$log_file" && echo "Network was not working for the previous $network_check_tries checks." >> "$log_file" && echo "Restarting ppp0" >> "$log_file"
killall pppd
sleep 20
/usr/sbin/pppd call gsm
sleep 120
# Then we check again if restarting wlan0 fixed the issue;
# if not we reboot as last resort
ping -c1 google.com
if [ $? -eq 0 ]
then
date >> "$log_file" && echo -e "-- Network is working correctly -- \n" >> "$log_file" && echo 0 > "$network_check_tries_file"
else
date >> "$log_file" && echo -e "-- Network still down after ifdownup... reboot time!-- \n" >> "$log_file" && echo 0 > "$network_check_tries_file" && echo "$(($N_reboots + 1))" > "$N_reboots_file" && reboot
fi
fi
ifupdown
如果您不使用它,为什么还要谈论?(2)为什么gateway_ip
从变量更改为硬编码常量?
network_check_tries_file
文件中的值(ping
失败时),也不会增加network_check_tries
变量。…(续)
network_check_tries
等于0、1 2、3、4、5和6 -测试仅在第七次调用(network_check_tries
等于6)时if [ "$network_check_tries" -gt 5 ]
成功。可以说,这是正确的行为。据脚本所知,网络可能在00:04:59中断,因此需要连续七次失败才能确保您覆盖了30分钟。…(续)