[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:
# 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
# for example stop the OpenVPN server, or reset the IPTables script.


NETWORKSCRIPT="/etc/init.d/networking restart"

# 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

# 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

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

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


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

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

Reply to: