#!/usr/bin/perl -- -*-CPerl-*- use strict; sub fail { print ::FH ($_ = localtime, " ", @_, "\n"); close ::FH; exit (1); }; sub current_default_gw { my ($gw, @lines, @matches); @lines = `/bin/netstat -nr`; @matches = grep (/^0\.0\.0\.0\s+(\S+)\s+/, @lines); scalar (@matches) || return; $matches[0] =~ /^0\.0\.0\.0\s+(\S+)\s+/; $gw = $1; return ($gw); }; sub dhcp_default_gw { my ($if) = shift (@_); my ($dhclient_file, $line, $gw, $file_if, @lines); $dhclient_file = "/var/lib/dhcp/dhclient-$if.leases"; $file_if = $if; @lines = `/bin/cat $dhclient_file`; foreach $line (@lines) { if ($line =~ /^\s*interface\s+"([^"]+)"/) { $file_if = $1; } elsif (($file_if eq $if) && ($line =~ /^\s*option\s+routers\s+(\d+\.\d+\.\d+\.\d+)/)) { $gw = $1; }; }; defined ($gw) || return; return ($gw); }; sub main { my ($if) = shift (@::ARGV); my ($current_gw, $dhcp_gw, $line, @lines); my ($ipwdir, @matches); my ($must_set) = 0; my ($must_unset) = 0; ($if =~ /^eth\d+/) || ($if =~ /^wlan\d+/) || return; open (::FH, ">>/var/log/ifup-local") || die ("Cannot open /var/log/ifup-local: $!"); defined ($if) || fail ("No argument to ifup-local"); ## See if default gw is not set $current_gw = current_default_gw(); defined ($dhcp_gw = dhcp_default_gw ($if)) || fail ("No option routers in /var/lib/dhcp/dhclient-$if.leases"); if (! defined ($current_gw)) { print ::FH ($_ = localtime, " no current default gw for $if\n"); $must_set = 1; } elsif ($current_gw ne $dhcp_gw) { print ::FH ($_ = localtime, " current gw $current_gw != dhcp gw $dhcp_gw for $if\n"); $must_set = 1; $must_unset = 1; }; if ((! $must_set) && (! $must_unset)) { print ::FH ($_ = localtime, " default gw already set to $dhcp_gw for $if\n"); return; }; if ($must_unset) { print ::FH ($_ = localtime, " removing default gw $current_gw for $if\n"); system ("/sbin/route", "del", "default", "gw", $current_gw); }; if ($must_set) { print ::FH ($_ = localtime, " adding default gw $dhcp_gw for $if\n"); system ("/sbin/route", "add", "default", "gw", $dhcp_gw); }; ## See if this was an ipw wireless startup and if an led exists @lines = ; if (scalar (@lines) == 1) { $ipwdir = $lines[0]; chdir ($ipwdir) || fail ("Cannot chdir to \"$ipwdir\": $!"); @lines = ; if ((scalar (@lines) == 1) && ($lines[0] eq "net:$if") && (-e "led")) { print ::FH ($_ = localtime, " turning on wireless LED"); open (::LED, ">led") && (print ::LED ("1")) && close (::LED); }; }; close (::FH); }; main();