Wireless-tools autodetect boot script
From ArchWiki
[edit] Please note there is now full wireless support in the standard initscripts - is this duplicate of this page?
- Find_Wireless.pl is a script to detect known networks you define.
- Notes:
- You would create this config somewhere on your machine. Then call it from your Find_Wireless.pl script at boot.
- Sample config:
[l0ser@~] : cat Scripts/Start_Home.wi0.sh #!/bin/sh echo "Starting eth0 prism wireless.. ." /usr/sbin/iwconfig eth0 essid NetGear /usr/sbin/iwconfig eth0 key restricted XXXX-XXXX-XXXX-XXXX-XXXX-XXXX-XX /usr/sbin/iwconfig eth0 nick archrules echo "Setting interface eth0: 192.168.0.52" /sbin/ifconfig eth0 192.168.0.53 netmask 255.255.255.0 /sbin/route add default gw 192.168.0.1 [l0ser@~] :
- Note: make sure hashed ESSID's matches your real ESSID on your Access Point.
- sample:
- If my home Access Point is "NetGear" then I would change the script to be:
my %Networks = (
"NetGear" => ["/home/cdowns/Scripts/Start_Hork_wi0.sh", "192.168.0.1", "192.168.0.50"],
- Here is The awesome perl Script.
- I would just call this from /etc/rc.local.
- Obviously make sure you: chmod 755 Find_Wireless.pl
#!/usr/bin/perl
## Find_Wireless.pl
## cdowns\@drippingdead.com
## ArchLinux ++
## IBM X40 ipw2200 using wireless-tools.
## Notes: Obviously wireless must be working before you use this
## script..
#
## Sample wireless script
#[l0ser@~/Scripts] : cat Start_Home.wi0.sh
##!/bin/sh
#
#echo "Starting eth0 prism wireless.. ."
#/usr/sbin/iwconfig eth0 essid My_Network
#/usr/sbin/iwconfig eth0 key restricted XXXX-XXXX-XXXX-XXXX-XXXX-XXXX-XX
#/usr/sbin/iwconfig eth0 nick archG
#echo "Setting interface eth0: 192.168.0.52"
#/sbin/ifconfig eth0 192.168.0.53 netmask 255.255.255.0
#/sbin/route add default gw 192.168.0.1
#[l0ser@~/Scripts] :
#use warnings;
use strict;
## Edit this.. Sample:
## add dns if you want it.
## "ESSID" => ["/path/to/network/script", "dns1", "dns1"]
my $interface = "eth0";
my %Networks = (
"Work" => ["/home/$USER/Scripts/Start_Work_wi0.sh", "192.168.1.1", "192.168.1.10"],
"Home" => ["/home/$USER/Scripts/Start_Home.wi0.sh", "192.168.0.1", "192.168.0.51"],
"Start-External" => ["/home/$USER/Scripts/Start-External.wi0.sh"]
);
my $int = "eth0";
my $wcmd = "/usr/sbin/iwlist $int scanning";
print "ESSID Networks =>\n";
&OutPut(`$wcmd`);
open(FILE, "/tmp/FindWireless.txt")
or die "Cannot open: $!\n";
while(<FILE>) {
(/^$interface/) && next;
(/^Cell/) && next;
if ( $_ =~ /ESSID:/ ) {
my ($s1, $s2) = split(/:/);
my ($s1m, $s2m ) = split(/"/);
my @essids;
chomp($s2m);
push(@essids, $s2m);
foreach my $essid ( @essids ) {
my (%Network, $Networks);
foreach my $Network ( keys %Networks ) {
if ( $Network =~ /$essid/ ) {
print "ESSID: '$Network'\n";
print "Wireless_Script: '$Networks{$essid}[0] '\n";
sleep 2;
print "Running Network Script.. \n";
system(`@{ $Networks{$essid} } 2>&1 /dev/null`);
&Create_DNS($Networks{$essid}[0], $Networks{$essid}[1], $Networks{$essid}[2]);
print "Done..\n";
}
}
}
}
}
close(FILE);
exit;
sub Create_DNS() {
my $Resolv = "/etc/resolv.conf";
my ($Networks, $essid);
($Networks{$essid}[0], $Networks{$essid}[1], $Networks{$essid}[2]) = @_;
print "Writing: $Networks{$essid}[1]\n";
print "Writing: $Networks{$essid}[2]\n";
if ( -e $Resolv ) {
unlink ( $Resolv );
}
open(FILE1, ">>$Resolv")
or die "Cannot open: $!\n";
print FILE1 "##generated by Find_Wireless.pl script\n";
print FILE1 "nameserver $Networks{$essid}[1]\n";
print FILE1 "nameserver $Networks{$essid}[2]\n";
close(FILE1);
}
sub OutPut() {
my $outputfile = "/tmp/FindWireless.txt";
if ( -e $outputfile ) {
unlink ( $outputfile );
}
my (@text) = @_;
open(OUTPUT, ">> $outputfile") || die "Can't Write LogFile: $!\n";
print OUTPUT @text;
close(OUTPUT);
}