[ltp] ideal network auto detection
Cameron McCormack
linux-thinkpad@linux-thinkpad.org
Thu, 22 Jul 2004 19:22:50 +1000
--X1bOJ3K7DJ5YkBrT
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Tino Keitel:
> The only problem is the use of the cable LAN instead of wireless if the
> former is available. Maybe this can be done with a script which brings
> down the WLAN if eth0 was successfully configured via DHCP. The pump
> DHCP client can call a script at certain events, so you can stop the WLAN
> if eth0 got a DHCP lease.
Yeah that's the sort of thing I do in my script. If it detects that the
Ethernet cable is plugged in, it will use arping to check it a known
host is on the LAN. If so, it'll use dhclient to get an address. If
the cable is not plugged in, it modprobes the wlan driver, gets a list
of nearby APs and chooses preferentially based on a list in the script.
If the cable gets plugged in at any time, the wireless is taken down and
it'll set up the Ethernet connection again.
Some of the script could be done without spawning extra processes (like
checking interface status), which I'll do when I get some time.
Cameron
--
Cameron McCormack
| Web: http://mcc.id.au/
| ICQ: 26955922
--X1bOJ3K7DJ5YkBrT
Content-Type: application/x-perl
Content-Disposition: attachment; filename="manage.pl"
Content-Transfer-Encoding: quoted-printable
#!/usr/bin/perl -w=0A=0Ause strict;=0A=0Amy @prefessids =3D ('Baroda');=0Am=
y %keys =3D ();=0A=0Amy $eth =3D 0;=0Amy $ath =3D 0;=0Amy $ap =3D '';=0Amy =
$essid =3D '';=0Amy $osdpid =3D 0;=0A=0A$SIG{HUP} =3D \&reload;=0Asub reloa=
d { $eth =3D $ath =3D 0; }=0A=0Aopen OSD, "| osd_cat -c green2 -s 2 -f '-*-=
helvetica-bold-r-*-*-34-*-*-*-*-*-*-*' -p top -A right -l 1";=0Aopen OSD2, =
"| DISPLAY=3D:0.1 osd_cat -c green2 -s 2 -f '-*-helvetica-bold-r-*-*-34-*-*=
-*-*-*-*-*' -p top -A right -l 1";=0A=0Asystem "ifconfig eth0 0.0.0.0 up";=
=0Asystem "modprobe -r ath_pci";=0A=0Afor (;;) {=0A # check=0A if ($eth) =
{=0A checketh();=0A }=0A if ($ath) {=0A checkath();=0A }=0A=0A pr=
int "eth is ", ($eth ? "UP" : "DOWN"), "\n";=0A if ($ath) {=0A print "a=
th is UP: $essid, $ap\n";=0A } else {=0A print "ath is DOWN\n";=0A }=
=0A=0A sleep 1;=0A=0A if (!$eth) {=0A tryeth();=0A }=0A=0A if (!$eth=
&& !$ath) {=0A tryath();=0A }=0A=0A}=0A=0Asub tryeth {=0A killdh('eth=
0');=0A system "ifconfig eth0 0.0.0.0 up";=0A if (system("ifconfig eth0 |=
grep RUNNING") =3D=3D 0) {=0A if (system("arping -0 -c 1 192.168.10.1")=
=3D=3D 0=0A || system("arping -0 -c 1 130.194.67.1") =3D=3D 0) {=0A=
system "modprobe -r ath_pci";=0A killdh('ath0');=0A $ath =
=3D 0;=0A $eth =3D system("dhclient -e -pf /var/run/dhclient.eth0.pid =
-lf /var/run/dhclient.eth0.leases eth0") =3D=3D 0;=0A if ($eth) {=0A =
message("Ethernet UP");=0A }=0A }=0A }=0A}=0A=0Asub checketh=
{=0A my $pid =3D `cat /var/run/dhclient.eth0.pid`;=0A chomp $pid;=0A if=
($pid eq ''=0A || !-f "/proc/$pid/exe"=0A || `readlink /proc/$pi=
d/exe` ne "/sbin/dhclient\n"=0A || system("ifconfig eth0 | grep RUNNIN=
G") !=3D 0) {=0A system "ifconfig eth0 0.0.0.0 up";=0A killdh('eth0')=
;=0A message("Ethernet DOWN");=0A $eth =3D 0;=0A } else {=0A $eth=
=3D 1;=0A }=0A}=0A=0Asub killdh {=0A my $if =3D shift;=0A my $pid =3D `=
cat /var/run/dhclient.$if.pid`;=0A chomp $pid;=0A if ($pid ne ''=0A =
&& -f "/proc/$pid/exe"=0A && `readlink /proc/$pid/exe` eq "/sbin/dhcli=
ent\n") {=0A kill 9, $pid;=0A }=0A}=0A=0Asub tryath {=0A killdh('ath0'=
);=0A print "Searching for APs...\n";=0A system "modprobe ath_pci";=0A s=
ystem "ifconfig ath0 0.0.0.0 up";=0A my @essids =3D ();=0A open FH, "iwli=
st scan 2>/dev/null | grep '^ *ESSID' |";=0A while (<FH>) {=0A if (/^ *=
ESSID:"(.*)"/) {=0A print " found: $1\n";=0A push @essids, $1;=
=0A }=0A }=0A close FH;=0A my $associated =3D 0;=0A my @tocheck =3D =
();=0A for (@prefessids) {=0A push @tocheck, $_;=0A }=0A for my $e (@=
essids) {=0A push @tocheck, $e unless grep { $_ eq $e } @tocheck;=0A }=
=0A for my $e (@tocheck) {=0A $essid =3D $e;=0A print "Trying to ass=
ociate with $essid...\n";=0A system "iwconfig ath0 essid \"" . quote($es=
sid) . "\"";=0A if (exists $keys{$essid}) {=0A system "iwconfig ath=
0 enc $keys{$essid}";=0A }=0A my $count =3D 5;=0A $associated =3D =
0;=0A while ($count > 0) {=0A my $line =3D `iwconfig ath0 2>/dev/nu=
ll | grep 'Access Point'`;=0A if ($? =3D=3D 0) {=0A $line =3D~ =
/Access Point: ([0-9A-Fa-f:]*)/;=0A $ap =3D $1;=0A }=0A pr=
int " try $count: $ap\n";=0A if ($ap !~ /ff:ff:ff:ff:ff:ff/i) {=0A =
$associated =3D 1;=0A last;=0A }=0A $count--;=0A =
sleep 1;=0A }=0A if ($associated) {=0A $ath =3D system("dhclien=
t -e -pf /var/run/dhclient.ath0.pid -lf /var/run/dhclient.ath0.leases ath0"=
) =3D=3D 0;=0A if ($ath) {=0A message("Wireless UP: $essid");=
=0A }=0A last;=0A } else {=0A $ap =3D '';=0A }=0A }=
=0A}=0A=0Asub checkath {=0A my $pid =3D `cat /var/run/dhclient.ath0.pid`;=
=0A chomp $pid;=0A if ($pid eq ''=0A || !-f "/proc/$pid/exe"=0A =
|| `readlink /proc/$pid/exe` ne "/sbin/dhclient\n"=0A || system("iwco=
nfig ath0 | grep -i 'Access Point: ff:ff:ff:ff:ff:ff'") =3D=3D 0) {=0A s=
ystem "modprobe -r ath_pci";=0A killdh('ath0');=0A message("Wireless =
DOWN");=0A $ath =3D 0;=0A } else {=0A $ath =3D 1;=0A }=0A}=0A=0Asub=
quote {=0A my $x =3D shift;=0A $x =3D~ s/\\/\\\\/g;=0A $x =3D~ s/"/\\"/=
g;=0A return $x;=0A}=0A=0Asub message {=0A my $message =3D shift;=0A pri=
nt OSD "$message\n";=0A print OSD2 "$message\n";=0A}=0A
--X1bOJ3K7DJ5YkBrT--