[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Network guard script



Hello guys,

I've created a script that guards the network. If the network is going
down, this script will restart the network script
(/etc/init.d/networking restart) and it will give you the option to
manually add some scripts te run when the network is down. I needed this
script, because the server we use, is in remote management by us. When
we do some risky things, like bridging with OpenVPN we don't like it if
the network is going down. So I created this script.

The script needs to be run in a cron job. I've added this script to our
server in a cronjob which runs each minute.

So what do you guys think of this script and do you think it's worth to
add it in Debian-edu as default?

This is the script:
------------------------------------------------------------------
#!/bin/bash
#
# Author: Michiel Eghuizen (Eduwijs)
# Date:   2006-05-16
#
# A tool which must be run in a cron job, to monitor the network.
# If the network is down, it will restart the network script,
# so you IP settings will be reloaded and it will execute some custom
commands,
# for example stop the OpenVPN server, or reset the IPTables script.

LOGFILE="/var/log/netguard.log"
CHECKIP="www.google.nl"

NETWORKSCRIPT="/etc/init.d/networking restart"
BRIDGEUTIL="/usr/sbin/brctl"
IPTABLESSAVE="/sbin/iptables-save"

# Commands to execute when the connection is down
# This will be run before the network script is restarten
execcommands () {
    /etc/init.d/openvpn stop &> /dev/null
}

# Check if logfile exists, if not then create a new one
if [ ! -e "$LOGFILE" ]; then
    touch $LOGFILE &> /dev/null
fi

# Check if i have write permissions
if [ ! -w "$LOGFILE" ]; then
    echo "You don't have write permissions to write to the log file."
    echo "Because the log file is in this script an important file, this
script will be closed."
    echo ""
    echo "Debug info:"
    echo "logfile: $LOGFILE"
    echo "user: " `whoami`

    exit 1
fi

ping -c 1 $CHECKIP > /dev/null

if [ "$?" != "0" ]; then
    echo "[NetGuard]" >> $LOGFILE
    echo "===============================" >> $LOGFILE
    echo "Status: Network is down!" >> $LOGFILE
    echo "Time: " `date` >> $LOGFILE
    echo "Current network configuration: " >> $LOGFILE
    /sbin/ifconfig >> $LOGFILE
   
    echo "Route configuration: " >> $LOGFILE
    /sbin/route >> $LOGFILE

    if [ -e "$BRIDGEUTIL" ]; then
        echo "Bridge configuration: " >> $LOGFILE
        $BRIDGEUTIL show >> $LOGFILE
    fi

    if [ -e "$IPTABLESSAVE" ]; then
        echo "IP tables: " >> $LOGFILE
        $IPTABLESSAVE >> $LOGFILE
    fi

    execcommands

    echo "Network script restart output: " >> $LOGFILE
    $NETWORKSCRIPT >> $LOGFILE

    ping -c 1 $CHECKIP > /dev/null
   
    if [ "$?" == "0" ]; then
        echo "Restart status: successful" >> $LOGFILE
    else
        echo "Restart status: FAILED" >> $LOGFILE
    fi
   
    echo "===============================" >> $LOGFILE
    echo "" >> $LOGFILE
fi



Reply to: