[SOLVED] List number of available open/wep/wpa networks from "iwlist scan"
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
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
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
@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
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:
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
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
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.
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
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:
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?
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.