All of lore.kernel.org
 help / color / mirror / Atom feed
From: Whit Blauvelt <whit@transpect.com>
To: lartc@vger.kernel.org
Subject: Re: [LARTC] Perl script to adjust default routing on 2 Net connections
Date: Wed, 20 Feb 2002 02:25:17 +0000	[thread overview]
Message-ID: <marc-lartc-101417195724834@msgid-missing> (raw)
In-Reply-To: <marc-lartc-101305340816319@msgid-missing>

Slightly updated - the "die" error catchers didn't always do what I
expected, so gone for now. Otherwise this has been behaving well.

Whit

--- cut --- 

#!/usr/local/bin/perl

# checkroutes.pl version 0.05, 18 Feb. 2002, whit@transpect.com
# changed from 0.04: (1) removed disfunctional "die" statements
#                    (2) added double check that for extra "default"

# Extends stock Nano-Howto setup with upstream route accessibility checking
# See http://www.linuxvirtualserver.org/~julian/nano.txt and patches at
# http://www.linuxvirtualserver.org/~julian/#routes
# (if not using Julian's patchs, remove "proto static" occurrences below)

# Run from cron (set with "crontab -e")
# example: "0-59/2 * * * * /path/to/checkroutes.pl" runs this every 2 minutes

# Best IPs to test may be last IPs at ISP before routing out to Net

# If perl not at /usr/local/bin/perl change first line to point to it

# Before running install Net::Ping from http://freeware.roobik.com/netping/
# (untar, cd to directory, "perl Makefile.PL; make; make install")

use Net::Ping;

# User settings
# _____________

$ip	= "/usr/sbin/ip";	# Location of "ip" on system

$gw1 	= "";	# Gateway 1 IP
$dev1	= "";		# Device (e.g., eth1)
$tip1	= "";	# Upstream IP to test
$src1	= "";	# Source address for outgoing packets
$wei1	= "";			# Weight

$gw2	= "";	# Gateway 2 IP
$dev2	= "";		# Device (e.g., eth2)
$tip2	= "";	# Upstream IP to test
$src2	= "";	# Source address for outgoing packets
$wei2	= "";			# Weight

$tab	= "222";		# Table to set routes in

$logfile = "/var/log/checkroutes.log"; # File to log messages to 

# create timestamp for log
# ________________________

sub TimeStamp {

	# subroutine to create a "nice" looking timestamp
	# that doesn't rely on the system's 'date' command

	my @days = ('Sun','Mon','Tue','Wed','Thu','Fri','Sat');
	my @months = ('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep',
		'Oct','Nov','Dec');

	my ($sec,$min,$hour,$dom,$mon,$year,$wday,$yday,$isd) = localtime(time);
	my $timestamp = sprintf("%3s %3s %02d %4d  %02d:%02d:%02d", $days[$wday],
		$months[$mon], $dom, $year+1900, $hour, $min, $sec);

	return $timestamp;
} 

# be sure routes are set via appropriate gateways to test IPs 
# ___________________________________________________________

$checkro = `$ip ro ls`;
if (!($checkro =~ /$tip1/)) {
	system ("$ip ro add to $tip1 via $gw1 src $src1 proto static");
}
if (!($checkro =~ /$tip2/)) {
	system ("$ip ro add to $tip2 via $gw2 src $src2 proto static");
}

# test if $gw1 and/or $gw2 currently in table $tab
# ________________________________________________

$checkgw = qx/$ip ro ls tab $tab/;

if ($checkgw =~ /$gw1/) {
	$check1 = 1;
} else {
	$check1 = 0;
}

if ($checkgw =~ /$gw2/) {
	$check2 = 1;
} else {
	$check2 = 0;
}

# ping the test IPs $tip1 and $tip2
# _________________________________

$p = Net::Ping->new("icmp");
if ($p->ping($tip1)) {
	$live1 = 1;
} else {
	$live1 = 0;
}
$p->close();
$p = Net::Ping->new("icmp");
if ($p->ping($tip2)) {
	$live2 = 1;
} else {
	$live2 = 0;
}
$p->close();

# If the current default in table $tab does not match the ping results then
# if only one route's test IP is pingable change the default to that route
# or if both test IPs are pingable change the default to use both
# or if neither test IPs are pingable change the default to use both 
# (might as well continue to try them, if we're not going anywhere anyway)
# _________________________________________________________________________

if (($check1 != $live1)||($check2 != $live2)) {
	if ((($live1 = 1)&&($live2 = 1))||(($live1 = 0)&&($live2 = 0))) {
		system ("$ip ro ap default tab $tab proto static nexthop via $gw1 dev $dev1 weight $wei1 nexthop via $gw2 dev $dev2 weight $wei2");
	} elsif (($live1 = 1)&&($live2 = 0)) {
		system ("$ip ro ap default tab $tab proto static via $gw1 dev $dev1");
	} elsif (($live1 = 0)&&($live2 = 1)) {
		system ("$ip ro ap default tab $tab proto static via $gw2 dev $dev2");
	} else {
		die ("value out of bounds\n");
	}
	system ("$ip ro del default tab $tab");
	system ("$ip ro flush cache");
}

# log
# ___

if (($live1 = 1)&&($live2 = 1)) {
	$log = "both UP";
} elsif (($live1 = 0)&&($live2 = 0)) {
	$log = "both DOWN (set UP)";
} elsif (($live1 = 1)&&($live2 = 0)) {
	$log = "$gw1 up, $gw2 DOWN";
} elsif (($live1 = 0)&&($live2 = 1)) {
	$log = "$gw2 up, $gw1 DOWN";
}

open(LOG, ">>$logfile");
print LOG &TimeStamp . " - $log\n";
close(LOG);

# double check that only one default is set
# _________________________________________

$checkdup = qx/$ip ro ls tab $tab/;
if ($checkdup =~ /default.*default/s) {
	system ("$ip ro del default tab $tab");
}
_______________________________________________
LARTC mailing list / LARTC@mailman.ds9a.nl
http://mailman.ds9a.nl/mailman/listinfo/lartc HOWTO: http://ds9a.nl/lartc/

  reply	other threads:[~2002-02-20  2:25 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-02-07  3:43 [LARTC] Perl script to adjust default routing on 2 Net connections Whit Blauvelt
2002-02-20  2:25 ` Whit Blauvelt [this message]
2002-02-20  4:58 ` Whit Blauvelt
2002-02-24 10:10 ` bert hubert

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=marc-lartc-101417195724834@msgid-missing \
    --to=whit@transpect.com \
    --cc=lartc@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.