Skip to content

Instantly share code, notes, and snippets.

@funkyremi
Created December 21, 2020 16:51
Show Gist options
  • Save funkyremi/ff999777d27ad893590c44b5cadc3430 to your computer and use it in GitHub Desktop.
Save funkyremi/ff999777d27ad893590c44b5cadc3430 to your computer and use it in GitHub Desktop.
Reconnect to wifi or ethernet when down
#!/bin/bash
LOGFILE=/var/log/network-monitor.log
if /sbin/ifconfig eth0 | grep -q "inet"; then
echo "$(date "+%m %d %Y %T") : Ethernet OK"
else
echo "$(date "+%m %d %Y %T") : Ethernet connection down! Attempting reconnection." >>$LOGFILE
/sbin/ifconfig eth0 up
OUT=$?
if [ $OUT -eq 0 ]; then
STATE=$(/sbin/ifconfig eth0 | grep "inet")
echo "$(date "+%m %d %Y %T") : Ethernet network connection reset." >>$LOGFILE
else
echo "$(date "+%m %d %Y %T") : Failed to reset ethernet connection" >>$LOGFILE
fi
fi
if /sbin/ifconfig wlan0 | grep -q "inet"; then
echo "$(date "+%m %d %Y %T") : Wifi OK"
else
echo "$(date "+%m %d %Y %T") : Wifi connection down! Attempting reconnection." >>$LOGFILE
/sbin/ifconfig wlan0 up
OUT=$?
if [ $OUT -eq 0 ]; then
STATE=$(/sbin/ifconfig wlan0 | grep "inet")
echo "$(date "+%m %d %Y %T") : Wifi network connection reset." >>$LOGFILE
else
echo "$(date "+%m %d %Y %T") : Failed to reset wifi connection" >>$LOGFILE
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment