LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-17-2010, 11:27 PM   #1
Steff86
LQ Newbie
 
Registered: Jul 2010
Distribution: Ubuntu mostly
Posts: 7

Rep: Reputation: 0
Post List number of available open/wep/wpa networks from "iwlist scan"


Hello

I don't have a problem but would like to have some feedback on the script below.
It basically reads out the number of WPA Networks, the number of encrypted Networks, and the number of unencrypted networks (ofcourse Wifi) from a iwlist scan

Problem was: WPA networks are marked in several ways (see WPA search terms in script) and were as well marked twice or just once with different terms. Thats why I check back on the cell variable because "Cell" indicates the next Network in the iwlist output. Therefore the script checks if it's in next network and doesn't set the count for wpa higher if it isn't.
I'm reading this out every 60s and have it written to a file which is then displayed in Conky on my desktop.
I merely searched around the net for different solutions and in the if clauses I tried around alot if to use "[" "{" "`" oder "'s so in the end I got it somehow working.

So but now my question: is there a better cleaner way to write this? Or do I habe some [{"' etc. too many?
I use this under Ubuntu 10.04.

Code:
#!/bin/bash
endless=0
until [ $endless = 1 ];do
wpa=0
off=0
on=0
cell=0
wep=0

while read line;do
[ "`echo $line | grep "Cell"`" ] && ((cell=0)) #setting "cell" to 0 if new network in the list
if [ `echo $line | grep -c "WPA Version 1"` = "1" ]; then
	if [ "$cell" = "0" ]; then
	let wpa=wpa+1 # only adding 1 to wpa count if cell is 0 then setting cell as 1
	let cell=1
	fi
fi	
if [ `echo $line | grep -c "WPA2 Version 1"` = "1" ]; then
	if [ "$cell" = "0" ]; then
	let wpa=wpa+1 #same as WPA (these could probably be merged but how exactly?)
	let cell=1
	fi
fi
[ "`echo $line | grep "Encryption key:off"`" ] && ((off++)) #listing of unencrypted networks
[ "`echo $line | grep "Encryption key:on"`" ] && ((on++)) #listing of encrypted networks
done < <(sudo iwlist wlan0 scan)

let wep=on-wpa  #wep Networks don't have any other markings except Enctyption key:on
echo "Available Networks:" > /home/steffen/.netconky #writing to file to be read out by Conky
echo "Open: $off WEP: $wep WPA: $wpa" >> /home/steffen/.netconky
sleep 60
done
Thanks for any feedback!
 
Old 07-18-2010, 12:55 AM   #2
sag47
Senior Member
 
Registered: Sep 2009
Location: Orange County, CA
Distribution: Kubuntu x64, Raspbian, CentOS
Posts: 1,860
Blog Entries: 36

Rep: Reputation: 458Reputation: 458Reputation: 458Reputation: 458Reputation: 458
You can merge the two if statements by using a regular expression shell pattern...

Code:
echo $line | grep -c "WPA[2]* Version 1"
or
Code:
echo $line | grep -c "WP[A2]* Version 1"
Google search shell regular expressions to learn more.
 
Old 07-18-2010, 01:48 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,715

Rep: Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034
Well the few things I see:

1. Use true or false in a while or until test instead of setting a useless variable

2. You are processing the scan output line by line but then applying grep counting. If it is only one line the answer is always 1/0 or true/false.
Why not instead use your grep count as an input into your variable?

3. Same as above for Encryption

Lastly I would mention that you may well be able to do all of this in awk as it is very good at processing data, especially when you know the format ahead of time

Hope that helps
 
Old 07-18-2010, 06:32 AM   #4
Steff86
LQ Newbie
 
Registered: Jul 2010
Distribution: Ubuntu mostly
Posts: 7

Original Poster
Rep: Reputation: 0
@sag47: thanks for the line, that makes it a lot shorter now

@grail:
ok I set the until-loop to [ 1=0 ] the variable was indeed useless

with the other two points I have some problems, could you give me a short line how I could use that (when I use "let" it doesn't take any commands (grep counting output) and keeps telling me it expects an operator)... which [ ` " do I use to tell it it should process the command first

an second I need to increase the wpa-count with the grep output and need to set cell variable to 1 in the same line (wouldn't make sense to grep twice for these variables), how do I do this

I now check first if cell=1 and only then grep for the searched values...makes more sense

thanks for the feedback

ps.: awk might be faster but I haven't really looked into it aand what I saw (I blindly used some awk commands from the net for another script) was pretty unselfexplaining

Last edited by Steff86; 07-18-2010 at 10:56 AM.
 
Old 07-18-2010, 10:13 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,715

Rep: Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034
What you are looking for with the grep count is called command substitution. Unfortunately I only have one access point to scan at home but the following
can demonstrate just as well:

input_file:
Code:
1 2 3
3 6 1
4 7 9
In your script you can do the following:
Code:
count=$(grep -c 1 input_file)
If you echo $count you will see it is set to the value 2

Quote:
ok I set the until-loop to [ 1=0 ] the variable was indeed useless
The words true and false actually exist in bash and do as you would expect. So you can have:
Code:
while ((true))
 
Old 07-18-2010, 10:55 AM   #6
Steff86
LQ Newbie
 
Registered: Jul 2010
Distribution: Ubuntu mostly
Posts: 7

Original Poster
Rep: Reputation: 0
well I the count itself is mostly clear, I could do this with the encryption of an encryption on
-> writing the scan to a file and then read out the file
problem is thet the marking of WPA networks isn't clear (either "WPA2 Version" or "WPA Version" or even both) so just counting these occurences in the file doesn't do the trick.
I think I have to search throught the scan line by line

I searched for a command where I grep in the string of a variable
so instead
count=$(grep -c 1 input_file)

count=$(grep -c 1 $line) but grep then searches for a file with the name of the string in the$line variable..is this even possible (have searched for an hour on the net and didn't find it)

I could count encryptions on/off in the whole scan but then I'd have to write two seperated ways for WPA-Encryption (with regard for the "Cell") and the general Encryption on/off strings

Thanks for the true/false hint.. I searched for exactly that term but only found 1=1 or 0=1 expressions! very good to know
 
Old 07-18-2010, 11:51 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,715

Rep: Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034
Well here is a slightly different way to approach it:
Code:
#!/bin/bash

while (( true ))
do
    sudo wlist scan wlan0 | awk 'BEGIN{FS="\n";RS=""}
                                 /key:off/{off++}
                                 /key:on/{on++}
                                 /WPA/{wpa++}
                                 END{ print "Available Networks:" > "/home/steffen/.netconky"
                                      print "Open: "off" WEP: "wpa - on" WPA: "wpa >> "/home/steffen/.netconky"
                                 }

    sleep 60
done
The nice thing here is that WPA will only be counted once per section. My assumption here is that the output of the scan
will leave a blank line between cells (I only have one so cannot confirm this).

If this is wrong please post what multiple cells looks like
 
Old 07-18-2010, 12:02 PM   #8
Steff86
LQ Newbie
 
Registered: Jul 2010
Distribution: Ubuntu mostly
Posts: 7

Original Poster
Rep: Reputation: 0
thanks a lot for this awk approach sadly there isn't an empty Line inbetween so multible networks look like this:
Code:
wlan0     Scan completed :
          Cell 01 - Address: 00:1F:F3:C3:E8:1C
                    Channel:8
                    Frequency:2.447 GHz (Channel 8)
                    Quality=34/70  Signal level=-76 dBm  
                    Encryption key:on
                    ESSID:"wifinetwork1"
                    Bit Rates:1 Mb/s; 2 Mb/s; 5.5 Mb/s; 11 Mb/s; 6 Mb/s
                              9 Mb/s; 12 Mb/s; 18 Mb/s
                    Bit Rates:24 Mb/s; 36 Mb/s; 48 Mb/s; 54 Mb/s
                    Mode:Master
                    Extra:tsf=000003734dcddd33
                    Extra: Last beacon: 32ms ago
                    IE: Unknown: 000B77696E6B656C6261756572
                    IE: Unknown: 010882848B960C121824
                    IE: Unknown: 030108
                    IE: Unknown: 0706444520010D0D
                    IE: Unknown: 2A0100
                    IE: Unknown: 32043048606C
                    IE: IEEE 802.11i/WPA2 Version 1
                        Group Cipher : TKIP
                        Pairwise Ciphers (2) : CCMP TKIP
                        Authentication Suites (1) : PSK
                    IE: Unknown: 2D1A2C0217FFFF000000000000000000000000000000000000000000
                    IE: Unknown: 3D1608000000000000000000000000000000000000000000
                    IE: WPA Version 1
                        Group Cipher : TKIP
                        Pairwise Ciphers (1) : TKIP
                        Authentication Suites (1) : PSK
                    IE: Unknown: DD180050F2020101010003A4000027A4000042435E0062322F00
                    IE: Unknown: DD1E00904C332C0217FFFF000000000000000000000000000000000000000000
                    IE: Unknown: DD1A00904C3408000000000000000000000000000000000000000000
                    IE: Unknown: DD07000393016A0120
                    IE: Unknown: DD0B0017F20100010100000007
          Cell 02 - Address: 64:16:F0:91:30:9E
                    Channel:3
                    Frequency:2.422 GHz (Channel 3)
                    Quality=23/70  Signal level=-87 dBm  
                    Encryption key:on
                    ESSID:"wifinetwork2"
                    Bit Rates:1 Mb/s; 2 Mb/s; 5.5 Mb/s; 6 Mb/s; 9 Mb/s
                              11 Mb/s; 12 Mb/s; 18 Mb/s; 24 Mb/s; 36 Mb/s
                              48 Mb/s; 54 Mb/s
                    Mode:Master
                    Extra:tsf=00000007e984d21a
                    Extra: Last beacon: 4248ms ago
                    IE: Unknown: 000B646670576972656C657373
                    IE: Unknown: 010C82848B8C12969824B048606C
                    IE: Unknown: 030103
                    IE: Unknown: 050402030000
                    IE: Unknown: 2A0100
                    IE: Unknown: 2F0100
                    IE: Unknown: DD090010180201F0000000
                    IE: WPA Version 1
                        Group Cipher : TKIP
                        Pairwise Ciphers (1) : TKIP
                        Authentication Suites (1) : PSK
Thanks now I try to figure exactly out what the awk command does ^^
 
Old 07-18-2010, 12:18 PM   #9
sag47
Senior Member
 
Registered: Sep 2009
Location: Orange County, CA
Distribution: Kubuntu x64, Raspbian, CentOS
Posts: 1,860
Blog Entries: 36

Rep: Reputation: 458Reputation: 458Reputation: 458Reputation: 458Reputation: 458
Since WPA networks are labeled but all networks are labeled as Encryption=on|off then why not take the difference.

For example lets have 10 networks 5 WPA1/2 encrypted, 3 WEP encrypted, and 2 unencrypted. Here's some pseudo for you.

$cells=grep Cell=10
$encrypted=grep Encrypted: on = 8
$wpa=grep WPA[1]*=5
$wep=$encrypted-$wpa = 3
$unencrypted=$cells-$encrypted=2

So basically you count up the cells, the encrypted status, and WPA networks. Then use basic arithmetic to determine the rest of the information you want to know.
 
Old 07-18-2010, 12:23 PM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,715

Rep: Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034
Thanks for that ... try this change to the above script:
Code:
RS=""
#change to
RS="Cell.*Address"
 
Old 07-18-2010, 12:26 PM   #11
Steff86
LQ Newbie
 
Registered: Jul 2010
Distribution: Ubuntu mostly
Posts: 7

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by sag47 View Post
$cells=grep Cell=10
$encrypted=grep Encrypted: on = 8
$wpa=grep WPA[1]*=5
$wep=$encrypted-$wpa = 3
$unencrypted=$cells-$encrypted=2
only Problem being that WPA networks are sometimes marked twice sometimes not (see cell 1 and cell 2 in scan file) cell 1 has twice written WPA* inside Cell 2 only once so I can't just count the numbers of WPA* in the file
and basically I just scan number og keyn nr of keyff and WPA and calculate number of WEP already (keyn - WPA)

or do you see another way that I can't see right now? thought a long time when I wrote the script so maybe I'm narrow sighted right now regarding the wpa scan point
 
Old 07-18-2010, 01:21 PM   #12
Steff86
LQ Newbie
 
Registered: Jul 2010
Distribution: Ubuntu mostly
Posts: 7

Original Poster
Rep: Reputation: 0
Thumbs up

ok lets leave out the loop
there were still one ' in the end and the "i" in iwlist missing, but it only runs through once so it doesn't start new and shows only on wpa network when there are 3 available

ok works when I put in:
Code:
RS="Address:"
and I have to set the variables to 0 upfront or there would be no number (eg for off) if there are no networks
so resulting in:
Code:
#!/bin/bash

off=0
wpa=0
on=0
wep=0
sudo iwlist wlan0 scan | awk 'BEGIN{FS="\n";RS="Address:"}
                                 /key:off/{off++}
                                 /key:on/{on++}
                                 /WPA/{wpa++}
                                 END{ print "Available Networks:"
                                      print "Open: "off" WEP: "wpa - on" WPA: "wpa""
                                 }'
I just printed it in terminal for test reasons

I think that's the nicest way possible or are there any other suggestions?

Thanks alot grail for the input!!!!!

EDIT: wow just figured that the variables are only internally in the awk so iff I set off=0 in th front awk doesn't use this variable
so do I just set off=0 inside the awk command?

EDIT2: ok inside the awk command obviously doesn't work because of the loop, it would be set to 0 every time so the count doesn't work...is there a way to show a "0" if there are no open networks listet in iwlist scan do so?

Last edited by Steff86; 07-18-2010 at 01:31 PM.
 
Old 07-18-2010, 10:42 PM   #13
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,715

Rep: Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034
You can set the defaults of all variables in the BEGIN{}. This is only ever run once at the start of the script. Likewise END is also only ever run once, at the end.
 
Old 07-19-2010, 04:08 PM   #14
Steff86
LQ Newbie
 
Registered: Jul 2010
Distribution: Ubuntu mostly
Posts: 7

Original Poster
Rep: Reputation: 0
Thumbs up wrap up of thread

Thanks Guys!!! Works like a charm!

and if anybody wants to use it here the complete optimized script:

Code:
#!/bin/bash
until (( false )); do
sudo iwlist wlan0 scan | awk 'BEGIN{off=0;on=0;wpa=0;FS="\n";RS="Address:"}
                                 /key:off/{off++}
                                 /key:on/{on++}
                                 /WPA/{wpa++}
                                 END{ print "Available Networks:"  > "/home/user/.netconky"
                                      print "Open: "off" WEP: "on - wpa" WPA: "wpa""  >> /home/user/.netconky"
                                 }'
sleep 60
done
you now could call .netconky in Conky and have the numbers of Networks displayed

I'll propably be back soon with more questions ^^ see you

Last edited by Steff86; 07-20-2010 at 11:48 AM. Reason: wep= on - wpa (not other way around) changed in script
 
  


Reply

Tags
conky, iwlist


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Why can I see wireless networks but never connect, WPA/WEP or open? Dan.b Linux - Newbie 3 09-03-2009 10:43 PM
Getting error while "iwlist scan" on Ubuntu. eaivision Linux - Laptop and Netbook 4 07-01-2009 12:05 PM
KWiFiManager - How to get the "Scan For Networks" to work Jim VE7ROX Linux - Newbie 2 03-30-2009 01:25 PM
ndiswrapper + bcmwl5, no scan results from "hidden" networks enigma_0Z Linux - Wireless Networking 2 10-06-2006 12:02 AM
is "iwlist scan" possible with Netgear MA401? dibblethewrecke Linux - Wireless Networking 0 05-17-2005 11:52 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:55 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration