<?xml version="1.0" encoding="windows-1252"?>
<node id="956275" title="heatblazer's scratchpad" created="2012-02-26 10:36:50" updated="2012-02-26 10:36:50">
<type id="182711">
scratchpad</type>
<author id="956274">
heatblazer</author>
<data>
<field name="doctext">
&lt;code&gt; #!/usr/bin/perl -w

use utf8;
use strict;
#subs here
#scan - match all networks available inside ESSID:" &lt;HERE&gt; " 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 message
						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 &lt;HERE&gt; 
#you can connect to the net here 
sub connecd {
	my @conn = @_;
	print "If network has password enter it: ";
	chomp(my $pawd = &lt;STDIN&gt;);
	print $conn[0], " you choose\n";
	my $connection = &amp;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 = &lt;STDIN&gt;);
	return $choice;
	}
#MENU 

sub menup {
	print &lt;&lt;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 = &amp;prompt("&gt;&gt; ");
	if ( $choice eq "connectme"  ) {
		print "Available networks.... \n";
		&amp;scan();
		}
	elsif ( $choice eq "killme" ) {
		system("killall dhcpcd");
		}
	elsif ( $choice eq "checkme" ) {
		if ( &amp;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";
		&amp;menup();
		}
	}
	
###############################################################################
my (@options) = @ARGV;
# do we use the right ones?
if ( $options[0] eq "-s" ) {
	&amp;menup();
	}
elsif ( $options[0] eq "-p" ) {
		#print to file
		}
else {
	print "Error command!\nPLease use -s for scan or -p for print to file(WIP)\n";
	}
&lt;/code&gt;</field>
</data>
</node>
