This Bash script monitors a specified network interface and ensures that certain routes are present when the interface is up. It continuously checks the connectivity of the interface and adds missing routes if necessary.
- Linux operating system
ping
commandip
command
- Clone the repository or download the
monitor_interface.sh
script.
git clone https://github.com/inabakumori/network-interface-monitor.git
- Make the script executable.
chmod +x monitor_interface.sh
-
Open the
monitor_interface.sh
script in a text editor. -
Modify the following variables according to your requirements:
MONITOR_IFACE
: Specify the network interface you want to monitor (e.g., "enp4s0").ROUTES
: Add or remove routes that should be present when the interface is up. Each route should be in the format "destination via gateway dev interface".TEST_IP
: Set the IP address or domain to check internet connectivity.DEFAULT_GATEWAY
: Set the default gateway IP address.
-
Save the changes.
- Run the script with root privileges.
sudo ./monitor_interface.sh
The script will start monitoring the specified interface and add missing routes if necessary. It will display the status of the interface ("up" or "down") and log any route additions with timestamps.
- To run the script automatically on system startup, add it to your crontab.
sudo crontab -e
Add the following line to the crontab file:
@reboot /usr/bin/screen -dmS monitor_interface /path/to/monitor_interface.sh
Replace /path/to/monitor_interface.sh
with the actual path to the script.
- Save the crontab file and exit the editor.
The script will now run automatically in the background whenever the system starts up.
This script has been successfully tested on Linux.
This project is licensed under the GNU General Public License v3.0. See the LICENSE file for details.
- inabakumori for creating the script.
If you have any questions or issues, please open an issue on the GitHub repository.