Perl Wireless Connection Script 14,965 views

A simple Linux Perl script that loads in the background and connects your wireless card to the internet. If no AP is found, it connects to ethernet. Includes automatic AP roaming support and ping detection.

  1#!/usr/bin/perl  2  3#### This script can be modified and redistrobuted but this  4#### message must not be removed. Proper credit must be given.  5  6# Wireless / Hardwire Internet Connection Script written by Chris Monahan.  7# Special thanks goes out to Carey for all the help! Please note that this  8# script requires pump, ifconfig, killall, and iwconfig + iwlist  9# (wireless-tools package) to operate correctly. Please make sure that 10# your SSID is broadcasted so that this script can detect your location. 11# Please visit corecoding.com for the latest version. 12 13 14# Load em up 15use strict; 16use Net::Ping; 17use POSIX 'setsid'; 18 19# Program Options 20my $conTries = (@ARGV[0])?@ARGV[0]:20; 21my $overrideDNS = 0; 22my $forkBG = 1; 23 24# Device Config 25my $wireless = 'eth1'; 26my $hardwire = 'eth0'; 27 28# Server Hostnames 29my $nameserver1 = '209.11.240.35'; 30my $nameserver2 = '209.11.240.36'; 31my $timeserver = 'time.nist.gov'; 32my $pingserver = 'www.somesteadywebsite.com'; 33 34# Command Line Utility Locations 35my $pump = "/sbin/pump"; 36my $ifconfig = "/sbin/ifconfig"; 37my $iwconfig = "/sbin/iwconfig"; 38my $iwlist = "/sbin/iwlist"; 39my $killall = "/usr/bin/killall"; 40my $resolvconf = "/etc/resolv.conf"; 41my $output = "/dev/null"; 42 43# Nearby Network Config 44my @networkConfig = ( 45  { mac => '01:14:DF:A6:EB:E8', ssid => 'ifly',      key => wep_key('burger'), found => 'home' }, 46  { mac => '0C:BC:51:83:67:C3', ssid => 'GetToWork', key => 'off',             found => 'work' }, 47  { mac => '0A:18:95:29:2C:1D', ssid => 'default',   key => '475ab32e89',      found => 'parents' }, 48); 49 50# Don't change these 51my $found; 52 53if ($forkBG) { 54  print "Internet Connection Script loaded in the background.\n"; 55 56  $SIG{CHLD} = sub { wait }; 57 58  defined (my $kid = fork) or die "Cannot fork: $!\n"; 59  if ($kid) { exit; } 60 61  open STDIN, ">$output"  or die "Can't read $output: $!"; 62  open STDOUT, ">$output" or die "Can't write to $output: $!"; 63 64  setsid or die "Can't start a new session: $!"; 65} 66 67# Catch errors from within system() executions 68open STDERR, ">$output" or die "Can't write to /tmp/log: $!"; 69 70# Main Code Starts Here 71print "Scanning for wireless networks"; 72system("$ifconfig $hardwire down"); 73system("$ifconfig $wireless up"); 74print "."; 75 76my $networks = `$iwlist $wireless scan`; 77 78for (@networkConfig) { 79  if ($networks =~ /$_->{mac}/ && !$found) { 80    system("$iwconfig $wireless essid " . $_->{ssid} . " key " . $_->{key}); 81    $found = $_->{found}; 82  } 83} 84 85print "."; 86if ($found) { 87  print "Configuring AP."; 88 89  system("$pump -i $wireless"); 90} else { 91  $found = 'hardwire'; 92 93  print "No APs found, trying hardwire"; 94  system("$ifconfig $wireless down"); 95  print "."; 96 97  system("$pump -i $hardwire"); 98} 99100if ($found) {101  print ".";102103  if ($overrideDNS) {104    open(fileOUT, ">$resolvconf");105    print fileOUT "nameserver $nameserver1\n";106    print fileOUT "nameserver $nameserver2\n";107    close(fileOUT);108  }109110  my $p = Net::Ping->new("tcp");111  my ($ret, $duration, $ip) = $p->ping($pingserver);112  print ".";113114  if ($ret == 1) {115    system("rdate -s $timeserver");116    #system("ntpdate $timeserver");117118    print "Connected";119    if ($found ne 'hardwire') {120      print " to $found!";121    } else {122      print "!";123    }124  } else {125    print "Not Connected!";126127    if ($conTries && $forkBG) { $conTries--; system("$0 $conTries"); }128  }129130  $p->close();131132  system("$killall -9 pump");133} else {134  print "Fatal Error!";135}136137print "\n";138139# Sub Routines #140sub wep_key {141  require Digest::MD5;142  return substr Digest::MD5::md5_hex( substr( shift() x 64, 0, 64 ) ), 0, 26;143}144145# terminate the process146CORE::exit(0) if ($forkBG);