aredn/files/usr/local/bin/connect

162 lines
3.5 KiB
Perl
Executable File

#!/usr/bin/perl -w
=for commnet
Part of BBHN Mesh -- Used for creating Amateur Radio friendly mesh networks
Copyright (C) 2015 Conrad Lara
See Contributors file for additional contributors
Copyright (c) 2013 David Rivenburg et al. BroadBand-HamNet
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation version 3 of the License.
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.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
=cut
$seq = 0;
sub tempfile
{
my $file;
do
{
$file = sprintf "/tmp/perlexec%d", ++$seq;
}
while(-e $file);
return $file
}
sub execute
{
my $file = tempfile();
open(EX, "> $file") or die;
print EX "@_";
close(EX);
my $rc = system "sh $file";
unlink $file;
return $rc;
}
sub branch
{
my $file = "/tmp/perlexec";
open(EX, "> $file") or die;
print EX "@_\n";
close(EX);
exec "sh $file";
}
$| = 1;
$ssid = shift;
$iface = "wlan0";
if(defined $ssid and $ssid eq "-h")
{
print "usage: connect [ssid]\n";
print "if no ssid is provided you will be given a choice of available AP's\n";
exit;
}
# do a site survey if ssid not given
if(not defined $ssid)
{
print "scanning...";
unlink "/tmp/wscan";
execute "wscan -i $iface -a -b -o -n 5 > /dev/null";
print "\n\n";
execute "cd /tmp; mv wscan wscan.tmp; sort -rn wscan.tmp > wscan; rm wscan.tmp";
open(FILE, "/tmp/wscan") or die "wscan failed\n";
while(defined ($line = <FILE>))
{
next if $line =~ /^\s*$/;
push @list, $line;
}
close(FILE);
die "no open access points found\n" if not @list;
print " Sig Rel Ch E SSID MAC Vendor\n";
print " --- --- -- - -------------------------------- ------------- ------\n";
for($i = 0; $i < @list; ++$i)
{
printf "%2d: %s", $i + 1, $list[$i];
}
print "\n";
while(1)
{
print "which access point [1] ? ";
$ans = <STDIN>;
die "connect aborted\n" if not defined $ans;
chomp $ans;
$ans = "1" if $ans eq "";
next if $ans !~ /^\d+$/;
next if $ans < 1 or $ans > @list;
last;
}
$ssid = substr $list[$ans - 1], 13, 32;
$ssid =~ s/\s+$//;
}
# associate
print "associating with $ssid ";
for($i = $done = 0; $i < 30 and not $done; ++$i)
{
print ".";
system "iwconfig $iface mode managed";
execute "iwconfig $iface essid '$ssid'";
sleep 1;
execute "iwconfig $iface | grep -q 'ESSID:\"$ssid\"'";
sleep 1;
execute "iwconfig $iface | grep -q 'Not-Associated'";
$done = 1 if $?;
}
print "\n";
die "association failed\n" if not $done;
# get a dhcp lease
print "requesting a dhcp lease\n";
execute "kill `cat /var/run/dhcp-wlan0.pid` 2>/dev/null" if -e "/var/run/dhcp-wlan0.pid";
$pid = fork;
if(!$pid)
{
branch "udhcpc -t 0 -i $iface -b -p /var/run/dhcp-wlan0.pid -R 2>/dev/null";
}
# check for an address
for($i = $done = 0; $i < 60 and not $done; ++$i)
{
sleep 1;
execute "ifconfig $iface | grep -q 'inet addr'";
$done = 1 if not $?;
}
if(not $done)
{
execute "kill `cat /var/run/dhcp-wlan0.pid` 2>/dev/null" if -e "/var/run/dhcp-wlan0.pid";
die "dhcp failed\n";
}
sleep 2;
print "connection successful\n";
exit 0;