Git Product home page Git Product logo

bash-network-repair-automation's Introduction

GitHub Issues Total Commits GitHub commit activity License Contributions welcome

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.

Tested Platforms

  • GNU/Linux, Debian 10 - Buster
  • Raspberry Pi, Raspbian 10 - Buster
  • ReadyNAS, 6.10.3

Prerequisites

GNU / Linux ping command is the only requirement and it should be available on most systems which already have networking.
In case your system does not have ping it is possible to install it using this command:
sudo apt-get install -y iputils-ping

How to use

  • Clone (or download) this repo locally:
    git clone https://github.com/ltpitt/bash-network-repair-automation.git
  • Make the script executable:
    sudo chmod +x network_check.sh
  • (Optional) If your network card name is not wlan0, you should replace it with the correct name. For example to use the name of the currently connected card:
    sed -i 's/wlan0/'"$(ip route get 1.1.1.1 | head -n1 | cut -d' ' -f5)"'/' network_check.sh
    (note for MacOS users, use gsed or add an argument sed -i '' ...)
  • Edit your root user's crontab using:
    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 the network is not working after the fix customize the reboot_server variable accordigly editing the script using vi (or your preferred editor):
    vi network_check.sh
  • The script contains VERY verbose comments and explanations in order to make it friendly and easy to modify for young and / or inexperienced BASH users. If you are an expert you will find those terrible and boring, here's a snippet to remove all comments in one go:
    grep -o '^[^#]*' network_check.sh

Optional - Push notifications / Email

If you want to add push or email notifications when your network is restored please check my other repo, Simple Notifications

Optional - Automatic repair with fsck in case of reboot

As a last thing, if you want to perform automatic repair with fsck in case of reboot (even if it slows down the boot a bit I think it is quite a good idea to do it) remember to enable the functionality on your system.
How to enable this funcionality changes according to your OS and its version, here's a possible way to configure it on Debian Buster (Raspberry Pi):

  • Edit /boot/cmdline.txt
  • Be sure that you have in it the following lines:
    fsck.mode=force
    fsck.repair=yes

You'll probably notice fsck.repair=yes is already there; these two properties are not exactly the same thing. From man systemd-fsck (these are actually parameters that are passed on by the kernel to init, i.e., systemd):

fsck.mode=

One of "auto", "force", "skip". Controls the mode of operation. The default is "auto", and ensures that file system checks are done when the file system checker deems them necessary. "force" unconditionally results in full file system checks. "skip" skips any file system checks.

fsck.repair=

One of "preen", "yes", "no". Controls the mode of operation. The default is "preen", and will automatically repair problems that can be safely fixed. "yes " will answer yes to all questions by fsck and "no" will answer no to all questions.

Release History

  • 0.1.1
    • Added multiple ip functionality for gateway check
  • 0.1.0
    • Removed all dependencies
    • Added reboot-loop prevention
  • 0.0.3
    • Ifupdown is not compatible with udev, in order to extend compatibility with other systems like NAS its requirement was made optional
    • Added logging
  • 0.0.2
    • Refactored in order to remove tmp files and preserve Raspberry's SD card
  • 0.0.1
    • First working version using tmp files to keep count of the network check retries

Meta

Davide Nastri โ€“ [email protected]

Distributed under the GPL license. See LICENSE for more information.

Bash Wifi Network Repair Script

Contributing

  1. Fork it (https://github.com/ltpitt/bash-network-repair-automation/fork)
  2. Create your feature branch (git checkout -b feature/fooBar)
  3. Commit your changes (git commit -am 'Add some fooBar')
  4. Push to the branch (git push origin feature/fooBar)
  5. Create a new Pull Request

Contributors (thanks, you are awesome! :) )

  1. czerwony03 -> bug: fixed wrong apostrophe
  2. pattyland -> Added absolute path if root does not have sbin in path
  3. deltabravozulu -> Make ifupdown optional for udev users
  4. mpatnode -> Use regular ping. Add reboot loop prevention
  5. m3at -> Allow for multiple check hosts/IPs
  6. pcchynoweth -> Feature/add note interface

bash-network-repair-automation's People

Contributors

czerwony03 avatar ltpitt avatar mpatnode avatar pattyland avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

bash-network-repair-automation's Issues

-mtime option on test for reboot

The find command on line 73 in network_check.sh uses the -mtime option with a value of 60 for reboot_cycle. When I read the documentation for the find command it looks like it won't reboot unless the last boot was more than 60 days ago, rather than 60 minutes ago (which I assume was the intention). I think the option should be -mmin

Check multiple network interfaces?

Hi, Is there some way to use this script with multiple interfaces?

I have an environment when ethernet might go offline quite frequently and would like it to check using wifi then before doing anything.

some time logs not saving in log file

WhatsApp Image 2022-01-29 at 9 07 45 PM

after 5 netcheck its successfully restarting wlan0, but in this case 23.26 watch this time in log file after this log next 6.24 in next day log is only there in log file,
what happen pi between 23.26 to 6.24 ?

note : i was tested this script in raspberrypi board runing 24/7.

`/bin/sh: 1: Cannot fork` Memory leak?

After a while the log file contained hundreds of /bin/sh: 1: Cannot fork lines.
The network connection is very spotty, so it's possible the script triggered a lot.

Running in a Pi Zero W 2 using 02c5fc5

5.10.63-v7+ #1496 SMP Wed Dec 1 15:58:11 GMT 2021 armv7l Raspbian GNU/Linux 10 (buster)

Reboot cycles are not prevented with file on /tmp

I have set the reboot_server=true. However, as the /tmp/.last_net_autoboot is on /tmp it gets removed on reboot and does not exist ever after the reboot. Hence, the reboot_cycle=60 is not really effective. /tmp is cleaned on reboot at least on Raspberry Pi OS.

Another note. The network_check_threshold=5 can be quite short. On my environment there might be easily four failures after the network reset by the script. The script is looping in some 20 seconds those attempts.

Sorry but ifupdown is not installed. Aborting.

This happens on rpi zero w, fixed by:
command -v /sbin/ifup >/dev/null 2>&1 || { echo >&2 "Sorry but ifupdown is not installed. Aborting."; exit 1; }

cat /etc/debian_version
10.1

also on rpi model b
cat /etc/debian_version
9.11

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.