#!/usr/bin/perl -w
use utf8;
use strict;
#subs here
#scan - match all networks available inside ESSID:" <HERE> " HERE
#You`ll see HERE in quotted marks and you can select the network to
#connect to that net
sub scan {
my $scanned = `iwlist scan`;
my @nets = ();
my @lines = split(/\n/, $scanned);
foreach my $word (@lines) {
my @words = split(/:/, $word);
foreach my $pat ( @words ) {
if ( $pat =~ m/"/i ) {
print "Pattern found: $pat \n"; #optional mess
+age
push(@nets, $pat);
}
}
}
return @nets;
}
#check connection
sub check {
my $con = `ping -c 1 yahoo.com`;
my @match = split(/ /, $con);
foreach (@match) {
if ( $_ =~ m/ttl/i || $_ =~ m/rtt/i) {
#find any ttl strings or rtt
return 1;
}
}
return 0;
}
#trims all quotes from the ESSID string
sub trim {
my @tr = @_;
my @trimmed = split(/"/, $tr[0]);
return @trimmed;
}
#connect to <HERE>
#you can connect to the net here
sub connecd {
my @conn = @_;
print "If network has password enter it: ";
chomp(my $pawd = <STDIN>);
print $conn[0], " you choose\n";
my $connection = &trim($conn[0]);
if (system("iwconfig wlan0 essid $connection key s: $pawd" ) ) {
return 1;
}
else {
return 0;
}
}
#PROMPT
sub prompt {
my @txt = @_;
print $txt[0];
chomp(my $choice = <STDIN>);
return $choice;
}
#MENU
sub menup {
print <<HERE;
Please select network by typing it`s name
exactly.
1)If you want to connect to the network
type "connectme"
(you might require a password )
2)If you want to stop all networks
type "killme".
3)If you want to view network status
type "checkme".
4)If you want to load a network from existing
nlog file type "loadme".
5)If you want to quit - type "exitme".
HERE
my $choice = &prompt(">> ");
if ( $choice eq "connectme" ) {
print "Available networks.... \n";
&scan();
}
elsif ( $choice eq "killme" ) {
system("killall dhcpcd");
}
elsif ( $choice eq "checkme" ) {
if ( &check() ) {
print "You are connected to the network\n";
}
else {
print "You are not connected \n";
}
}
else {
print "Error! I don`t know what [ $choice ] means ..\n";
&menup();
}
}
######################################################################
+#########
my (@options) = @ARGV;
# do we use the right ones?
if ( $options[0] eq "-s" ) {
&menup();
}
elsif ( $options[0] eq "-p" ) {
#print to file
}
else {
print "Error command!\nPLease use -s for scan or -p for print to f
+ile(WIP)\n";
}
|