Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# Raspberry Pi (and GNU / Linux) - Wifi Repair Automation
> This bash script checks the health status for wireless internet connection and, if it is failing, tries to fix it.
# Network Repair/Reboot Automation
> This bash script checks the health status for either wired or wireless internet connection and, if it is failing, tries to fix it.

## Prerequisite - Mandatory
## Tested Platforms
Raspberry Pi Linux
ReadyNAS 6.10.3

- Download and install fping:
`sudo apt-get install fping -y`
## Prerequisites

## Prerequisite - Optional
You at least need the "ping" command which should exist on most systems which already have networking. If not the following should get you there:

- Ifupdown isn't compatible with udev and on some systems (e.g. your NAS OS) it may not be a good idea replacing it. The script will detect automatically ifupdown and will work also if it is not installed. In the next releases of this script ifupdown usage will be deprecated.
`sudo apt-get install ifupdown -y`
`apt-get install -y iputils-ping`

## How to use

Expand All @@ -19,7 +19,7 @@
`sudo crontab -e`
- Add to your crontab the following line, it will execute the check every minute. Please customize the script path according to the folder where you cloned the repo:
`* * * * * /yourpath/network_check.sh >> /var/log/netcheck.log 2>&1`
- If you also want to reboot in case wifi is not working after the fix customize the reboot_server variable accordigly editing the script:
- If you also want to reboot in case the network is not working after the fix customize the reboot_server variable accordigly editing the script:
`nano network_check.sh`

## Optional - Push notifications / Email
Expand Down
52 changes: 24 additions & 28 deletions network_check.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,23 @@
#
# Please check README.md for install / usage instructions

# Checking if requirements (fping and ifupdown) are installed
`command -v /sbin/fping >/dev/null 2>&1 || command -v fping >/dev/null 2>&1` || { echo >&2 "Sorry but fping is not installed. Aborting."; exit 1; }

command -v /sbin/ifdown >/dev/null 2>&1 || command -v ifup >/dev/null 2>&1
USE_IFUPDOWN=$?

###
# Configuration variables, customize if needed
###

# Set gateway_ip to the gateway that you want to check to declare network working or not
gateway_ip='1.1.1.1'
# Set nic to your Network card name, as seen in ifconfig output
# Set nic to your Network card name, as seen in ip output
nic='wlan0'
# Set network_check_threshold to the maximum number of failed checks
network_check_threshold=5
# Set reboot_server to true if you want to reboot as a last
# option to fix wifi if ifdown / ifup failed
# option to fix wifi if ip up/down fail
reboot_server=false
# to prevent reboot loops, only reboot once every N minutes
reboot_cycle=60
# last boot file
last_bootfile=/root/.last_net_autoboot

###
# Script logic
Expand All @@ -36,27 +34,26 @@ function date_log {
}

function restart_wlan {
# Trying wlan restart using ifdown / ifup
# Trying wlan restart using ip
date_log "Network was not working for the previous $network_check_tries checks."
date_log "Restarting $nic"
if [[ $USE_IFUPDOWN = 0 ]]; then
/sbin/ifdown "$nic"
sleep 5
/sbin/ifup --force "$nic"
sleep 60
else
/sbin/ifconfig "$nic" down
sleep 5
/sbin/ifconfig "$nic" up
sleep 60
fi
/sbin/ip link set "$nic" down
sleep 5
/sbin/ip link set "$nic" up
sleep 60

# If network is still down and reboot_server is set to true reboot
host_status=$(fping $gateway_ip)
if [[ $host_status != *"alive"* ]]; then
if [ "$reboot_server" = true ] ; then
date_log "Network is still not working, rebooting"
/sbin/reboot
ping -c 1 $gateway_ip > /dev/null 2>&1
if [[ $? != 0 ]]; then
if [ "$reboot_server" = true ]; then
# if there's no last boot file or it's older than reboot_cycle
if [[ ! -f $last_bootfile || $(find $last_bootfile -mtime +$reboot_cycle -print) ]]; then
touch $last_bootfile
date_log "Network is still not working, rebooting"
/sbin/reboot
else
date_log "Last auto reboot was less than $reboot_cycle minutes old"
fi
fi
fi
}
Expand All @@ -65,12 +62,11 @@ function restart_wlan {
# network_check_threshold failures the network will be declared as
# not functional and the restart_wlan function will be triggered
while [ $network_check_tries -lt $network_check_threshold ]; do
# Checking if ping to gateway is working using fping
host_status=$(fping $gateway_ip)
# Increasing network_check_tries by 1
network_check_tries=$[$network_check_tries+1]

if [[ $host_status == *"alive"* ]]; then
ping -c 1 $gateway_ip > /dev/null 2>&1
if [[ $? = 0 ]]; then
# Network is up
date_log "Network is working correctly" && exit 0
else
Expand Down