Command Line Connection Management

Maintaining Linux Network Connections

Modern linux distributions usually come with some kind of network manager installed by default. The most common is NetworkManager. I have also used wicd in the past. But here is the way to make this work using cli tools only.

Why do this? One reason is that the cli way is more flexible. The graphical network interface managers do not always allow you to have multiple devices configures. At least I could not figure out how to make this happen. Also, doing this the cli way means typing in vi rather than clicking.

First, install wpasupplicant if you have wireless interfaces:

sudo apt install wpasupplicant

Now modify /etc/network/interfaces to inlude all of the interfaces you plan to use. Here is an example:

# This file describes the network interfaces available on your system # and how to activate them. For more information, see interfaces(5). source /etc/network/interfaces.d/* # The loopback network interface auto lo iface lo inet loopback # cat5 eno1 allow-hotplug eno1 iface eno1 inet dhcp # wifi wlp12s0 allow-hotplug wlp12s0 iface wlp12s0 inet dhcp wpa-ssid myssid wpa-psk mypassphrase

Since this file will contain login credentials for your wireless network, you may wish to also chmod 600 /etc/network/interfaces.

Now to the monitoring script. Modify it to reflect the ip addess of your router as well as your interfaces.

#!/usr/bin/bash # Define IP of router as well as alist of interfaces ROUTERIP='192.168.0.1' IFACES=( 'eno1' 'wlp12s0' ) # Test for total network failure x=`ping $ROUTERIP -W 3 -c 3 | grep '3 received'` if [ "$x" = "" ]; then # Now go through interfaces individually for IFACE in "${IFACES[@]}"; do x=`ping -I $IFACE -W 3 -c 3 $ROUTERIP | grep '3 received'`; if [ "$x" = "" ]; then /usr/sbin/ifdown $IFACE (/usr/sbin/ifup $IFACE && /usr/sbin/dhclient $IFACE) & fi done; fi

Just save this as something like /root/netmgr.sh, chmod +x, and then schedule it in root's crontab.

home