Linux Training

Linux training for private, public & voluntary sector.

0793 572 8612

City LinUX sample scripts - iptkovr


#!/bin/bash
# $Id: iptkovr,v 1.23 2014/05/23 14:08:24 fulford Exp $
# $Source: /src/merlin/usr/local/etc/RCS/iptkovr,v $
# $Revision: 1.23 $
# Author C W Fulford.
# Copyright 2014 (c) C W Fulford.
# Licensed for public use under the LGPL, .
# For assistance contact fulford@fulford.net 0709 229 5385
########################################################################
cmd=`basename $0`
fcmd="$@"
syntax="$cmd [-c <config_file>] [-d] [-v] <target>"
while [ $# -gt 0 ] ;do
	case $1 in 
		-c) config=$1;shift 2;;
		-d) debug=:;set -x;shift;;
		-v) verbose=:;shift;;
		 *) target=$1;shift;;
	esac
done
config=${config:-"/usr/local/etc/iptkovr.cf"}
[ -f $config ] ||{
	echo "$cmd: can't find $config" >&2
	echo "$cmd: closing down" >&2
	exit 1
}
eval `sed -ne "/$id:/,/^$/{
			/^[ 	][^#]*/p
	}" $config`
id=`id -un`
ok=:
cloneip=`getent hosts $clone|awk '{print $1}'`
targetip=`getent hosts $target|awk '{print $1}'`
txt=00022220009@txtservice.co.uk

[ $verbose ] && {
	echo "$cmd: monitoring $target on $targetip"  >&2
}
if ping -c 1 $cloneip >/dev/null 2>&1 ;then
	[ $verbose ] && echo "$cmd: $clone at $cloneip on standby">&2
else
	echo "$cmd: standby $clone down - terminating" >&2
	exit 1
fi
while [ $ok ];do
	if ping -c 1 $targetip >/dev/null 2>&1;then
		sleep $sleep
	else
		[ $verbose ] && {
			echo "$cmd: $target not responding on $targetip" >&2
		}
		sleep $sleep
		if ping -c 1 $targetip >/dev/null 2>&1 ;then
		   [ $verbose ]&&{
			echo "$cmd: hiccup only, monitoring $target on $targetip">&2
		   }
		else	
			unset ok		
		fi
	fi
done
msg="$target down - activating $targetip on $clone"

[ $verbose ]&& echo "$cmd: $msg" >&2

logger  -p local0.alert "$msg"

#mailx -s "URGENT $target down" -c $txt $id <<- .
mailx -s "URGENT $target down"  $id <<- .
	$msg
.

ssh -t $cloneip "sudo ifconfig $vif $targetip/24" 2>/dev/null
sleep 3
if ping -c 1 $targetip >/dev/null 2>&1 ;then
	echo "$cmd: ip takeover of $targetip complete"
else
	echo "$cmd: ip takeover of $targetip failed" >&2
fi
[ $verbose ] && echo "$cmd: terminating">&2


######################################################################
# This program is free software: you can redistribute it and or      #
# modify it under the terms of the Lesser GNU General Public License #
# as published by the Free Software Foundation, either version 3 of  #
# the License, or (at your option) any later version.                #
#                                                                    #
# This program is distributed in the hope that it will be useful,    #
# but WITHOUT ANY WARRANTY; without even the implied warranty of     #    
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the      #
# GNU General Public License for more details.                       #
#                                                                    # 
# A copy of the Lesser GNU General Public License and the GNU        #
# General Public License is available at                             #
# <http://www.gnu.org/licenses/>.                                    #
######################################################################

The layout and associated style sheets for this page are taken from the World Wide Web Consortium and used here under the W3C software licence.