#!/usr/bin/perl
#### This script can be modified and redistrobuted but this #### message must not be removed. Proper credit must be given.
# Wireless / Hardwire Internet Connection Script written by Chris Monahan. # Special thanks goes out to Carey for all the help! Please note that this # script requires pump, ifconfig, killall, and iwconfig + iwlist # (wireless-tools package) to operate correctly. Please make sure that # your SSID is broadcasted so that this script can detect your location. # Please visit corecoding.com for the latest version.
# Load em up use strict; use Net::Ping; use POSIX 'setsid';
# Program Options my $conTries = (@ARGV[0])?@ARGV[0]:20; my $overrideDNS = 0; my $forkBG = 1;
# Device Config my $wireless = 'eth1'; my $hardwire = 'eth0';
# Server Hostnames my $nameserver1 = '209.11.240.35'; my $nameserver2 = '209.11.240.36'; my $timeserver = 'time.nist.gov'; my $pingserver = 'www.somesteadywebsite.com';
# Command Line Utility Locations my $pump = "/sbin/pump"; my $ifconfig = "/sbin/ifconfig"; my $iwconfig = "/sbin/iwconfig"; my $iwlist = "/sbin/iwlist"; my $killall = "/usr/bin/killall"; my $resolvconf = "/etc/resolv.conf"; my $output = "/dev/null";
# Nearby Network Config my @networkConfig = ( { mac => '01:14:DF:A6:EB:E8', ssid => 'ifly', key => wep_key('burger'), found => 'home' }, { mac => '0C:BC:51:83:67:C3', ssid => 'GetToWork', key => 'off', found => 'work' }, { mac => '0A:18:95:29:2C:1D', ssid => 'default', key => '475ab32e89', found => 'parents' }, );
# Don't change these my $found;
if ($forkBG) { print "Internet Connection Script loaded in the background.\n";
$SIG{CHLD} = sub { wait };
defined (my $kid = fork) or die "Cannot fork: $!\n"; if ($kid) { exit; }
open STDIN, ">$output" or die "Can't read $output: $!"; open STDOUT, ">$output" or die "Can't write to $output: $!";
setsid or die "Can't start a new session: $!"; }
# Catch errors from within system() executions open STDERR, ">$output" or die "Can't write to /tmp/log: $!";
# Main Code Starts Here print "Scanning for wireless networks"; system("$ifconfig $hardwire down"); system("$ifconfig $wireless up"); print ".";
my $networks = `$iwlist $wireless scan`;
for (@networkConfig) { if ($networks =~ /$_->{mac}/ && !$found) { system("$iwconfig $wireless essid " . $_->{ssid} . " key " . $_->{key}); $found = $_->{found}; } }
print "."; if ($found) { print "Configuring AP.";
system("$pump -i $wireless"); } else { $found = 'hardwire';
print "No APs found, trying hardwire"; system("$ifconfig $wireless down"); print ".";
system("$pump -i $hardwire"); }
if ($found) { print ".";
if ($overrideDNS) { open(fileOUT, ">$resolvconf"); print fileOUT "nameserver $nameserver1\n"; print fileOUT "nameserver $nameserver2\n"; close(fileOUT); }
my $p = Net::Ping->new("tcp"); my ($ret, $duration, $ip) = $p->ping($pingserver); print ".";
if ($ret == 1) { system("rdate -s $timeserver"); #system("ntpdate $timeserver");
print "Connected"; if ($found ne 'hardwire') { print " to $found!"; } else { print "!"; } } else { print "Not Connected!";
if ($conTries && $forkBG) { $conTries--; system("$0 $conTries"); } }
$p->close();
system("$killall -9 pump"); } else { print "Fatal Error!"; }
print "\n";
# Sub Routines # sub wep_key { require Digest::MD5; return substr Digest::MD5::md5_hex( substr( shift() x 64, 0, 64 ) ), 0, 26; }
# terminate the process CORE::exit(0) if ($forkBG);
|