#!/usr/bin/perl # Run scripts that do network stuff for intermittent 802.11 connections # configuration $if = 'eth0'; $timeout = 6; $queue = '/var/spool/burst'; # programs $ifconfig = '/sbin/ifconfig'; $iwconfig = '/usr/sbin/iwconfig'; $iwgetid = '/usr/sbin/iwgetid'; $dhcpcd = '/sbin/dhcpcd'; sub process_queue { @children = (); opendir QUEUE, $queue; @jobs = grep {/^[^.]/} readdir(QUEUE); closedir(QUEUE); print "forking "; foreach $job (@jobs) { print "$job "; $x = fork(); if ($x) { push(@children,$x); } else { exec("$queue/$job"); print "exec failed: $?\n"; exit(1); } } print "\n"; print "Waiting for all children to complete...\n"; foreach $child (@children) { waitpid $child, 0; print "$child exited\n"; } } sub ifreset { system("$ifconfig $if down"); sleep(1); system("$ifconfig $if up"); } # Reset the interface. &ifreset; # Wait for an ESSID to show up while (1) { $cell = (split(/ +/,`$iwgetid --ap $if`))[3]; chomp $cell; next if ($cell eq $prevcell); if ($cell ne '00:00:00:00:00:00') { # Got a live one... `$iwgetid $if` =~ /ESSID:"(.*)"/; $essid = $1; print "Found $essid - $cell. Attempting DHCP...\n"; # Lock in the essid system("$iwconfig $if essid $essid"); $n = system("$dhcpcd -t $timeout $if"); $exit = $n >> 8; if ($exit == 0) { # success! process the queue! $ENV{'ESSID'} = $essid; $ENV{'CELL'} = $cell; &process_queue; delete $ENV{'ESSID'}; delete $ENV{'CELL'}; print "Done.\n"; } else { print "DHCP failed.\n"; &ifreset; } } else { # Lost connection. Shut it down. system("killall dhcpcd"); &ifreset; } # unlock essid system("$iwconfig $if essid any"); $prevcell = $cell; sleep(1); }