You are not logged in.

#1 2009-10-11 04:17:15

elvishandrew
Member
Registered: 2009-10-11
Posts: 1

[SOLVED] Newbie Networking Perl Script

Hello everyone,
Ive just started using Arch, and this is my first time posting. I am also trying to learn perl. I thought a good project would be to write a script to automate connecting to wireless networks. So far it only works on unencrypted networks, but that feature will be implemented in the future. Any input/constructive criticism would be appreciated, especially on writing programs "the Unix way" and using good "Perl form". I want to become a Unix admin someday, so this is great practice for me. Thank you.

#!/usr/bin/perl -w
# File: bestWireless.pl
# Author: elvishandrew
# Description: New Version of wireless.pl implementing hash to keep track of best wifi signal strength
# Last Modified: September 30, 2009
use strict;

my $wifiIn = "ra0"; #change this if you have a different wireless interface.
my $dhcpCommand = "dhcpcd"; #change this if you use a different command ie: ubuntu uses "dhclient"

#######################
`killall $dhcpCommand`; #kill the dhcp process if it is running
`ifconfig $wifiIn up`; #bring the wireless interface up
#######################

 #uses the system call to populate an array of the wireless scan
my @wList = `iwlist $wifiIn scan`;

#wifiHash will be a hash of hashes
#where the wifiHash of element essid matches the word 
#'mac' => variable mac and 'quality' => varible quality
my %wifiHash; 
my $mac;
my $essid;

foreach my $x (@wList){

    #match the mac address after "Address"
    if($x =~ /Address\:\s+([\w\:]+)/) { 

        #save mac address
        $mac = $1; 

    #match the essid in the " "
    }elsif($x =~ /ESSID\:\"(.+)\"/){ 
        $essid = $1;

    #match the signal stringth ex: "55"/100 (only match the 55)
    }elsif($x =~ /Quality\:([\d]+)\//)
    {
        #if the value is not null
        if($wifiHash{$essid}->{'quality'}) 
        {
            #if the current quality for an essid is greater than the hash essid -> quality    
            if($1 > ($wifiHash{$essid}->{'quality'}))
            {
                #set the $essid to equal the mac and quality
                $wifiHash {$essid}->{'mac'} = $mac;
                $wifiHash{$essid}->{'quality'} = $1;
            }
        
        }
        #there is no stored quality for $essid
        else{
                $wifiHash {$essid}->{'mac'} = $mac;
                $wifiHash{$essid}->{'quality'} = $1;


        }
    }
        

}

my $count = 0;
my $choice;
my %choiceHash;

print "\tEssid\t\tMac\t\t\tQuality\n";
print "------------------------------------------------------\n";

#this loop is used to print the $essid for %wifiHash and count the number of essids' in the hash
while( my($eid) = each %wifiHash){
    $count += 1;
    $choiceHash{$count} = $eid;
    print "$count:\t$eid\t" . $wifiHash{$eid}->{'mac'} ."\t". $wifiHash{$eid}->{'quality'}. "\n"; 
    print "\n";
    }

print "What is your choice 1 - " . ($count) . "? ";
chomp($choice = <STDIN>); 

#create a variable which is the mac of the user input $choice
my $macSet = $wifiHash{($choiceHash{$choice})}->{'mac'};
###################################
#bring up the wireless interface for the specified mac address
`iwconfig $wifiIn ap $macSet`;
#get an ip address
`$dhcpCommand $wifiIn`;
###################################

Last edited by elvishandrew (2010-04-04 23:51:05)

Offline

Board footer

Powered by FluxBB