Help needed for using awk to parse a file to make array for bash script
I am trying to write a script that will run "iwlist <interface> scanning >> iwlist-file."
Then I want to parse the file so that the ESSID, AP, encryption key off vs on, and signal strength all become variables that I will be able to use in the script. I will have some preferred networks with special static vs dhcp connections that if that ESSID is available, a different script will run (say in same directory named by network) but if no network is found, it would prioritize the available networks with Encryption key off by signal strength and connect automatically to it. The file I need to parse to obtain those variables would look something like this: Code:
BigDog tallmtt # iwlist ath0 scanning |
Something like this:
Code:
$ ESSID=(`grep ESSID: filename|sed -e 's/ //g'|cut -c7-|xargs`) |
Thanks for your quick reply. That lists the available networks, but I want to be able to somehow associate the ESSID with its specific AP, key (off or on), and signal strength.
Maybe breaking up the file into separate temporary files in /tmp/ with that info? maybe awk array? I don't know how todo either and am quite a novice at scripting. Thanks again for your help. |
Probably way convoluted but with
Code:
#!/bin/sh Here's some links to BaSH scripting guides: http://www.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html, http://www.tldp.org/LDP/Bash-Beginne...tml/index.html (and http://www.tldp.org/LDP/abs/html/). |
I am blind.
May be one day I will have vision. |
Quote:
Code:
iwlist ath0 scanning| awk '/Cell/ {print $5}; /ESS/ {print substr($1,7)}; /Freq/ \ |
Quote:
Code:
iwlist ath0 scanning | awk -F '[ :=]+' '/(ESS|Freq|Qual)/{ printf $3" " } |
You all are great! This command outputs:
Code:
# iwlist ath0 scanning | awk -F '[ :=]+' '/(ESS|Freq|Qual)/{ printf $3" " } /Encr/{ print $4 }' Thanks for your help - this gives me the setup I need to make an associative array in awk. My next step is to make each line a field in the array. I will work on this tomorrow and if I need help will post that as well. Any suggestions are always welcome. |
Quote:
Code:
/Cell/{printf substr($0,30)" "} Quote:
Code:
# iwlist ath0 scanning | awk -F '[ :=]+' '/(ESS|Freq|Qual)/{ printf $3" " } /Encr/{ print $4 }' Code:
# iwlist ath0 scanning | awk -F '[ :=]+' '/(ESS|Freq|Qual)/{ printf $3" " } /Encr/{ print $4 }' | sort -k4 -k3nr |
Hello. I found this older thread by searching via Google and it has been a great help to me as I learn scripting. However, I must be missing something, because I still don't see how these examples make the results available as variables. I'd like to take the results and put them into an Xdialog menu so that the user could select which network to connect to, then pass the results to the iwconfig command based on the user's selection. I find that if I use iwconfig, I can connect more reliably than via wicd or WiFi Radar, neither of which seem to work very well in my configuation.
I've purchased some books on bash scripting, but I'm at a wall on the variable array technique. I'd appreciate any advice. |
Well, if you can print out the information you need, you can also put it into variables, for instance by using "read":
Code:
print whatever | while read var1 var2 ...; do whatever; done |
I wish I searched for this thread instead of reinventing the wheel which took me weeks while holding a crying newborn. But it was great to learn the basics of awk and grep.
I am a recent migrant from Windows where I was using Netstumbler to align high gain antennas. I haven't found a simple tool in Linux that could provide a graphical feedback of signal level so I set to make my own script for this. I just need some direction, not asking anyone to write this for me. For illustration the on-screen feedback would look something like this: "MyWireless" 00:21:1E:41:E9:50 Channel:6 Signal Level -59 on =================================================================== "Hotspot" 00:21:1E:41:E9:40 Channel:6 Signal Level -70 off ======================== ... The idea came from Wavemon except this one would show all non-associated APs. I figured out how to make an animated bar and how to parse the output of 'iwlist scanning' and get the numerical values I need. I am having trouble separating the iwlist output's cells so I can feed one number at a time to the part of the script that's doing the graphical bar feedback. For example: Code:
# extract Signal Level I haven't studied in detail how 'iwlist scanning' sorts out the cell output. They are not sorted by signal quality, level, etc. I am guessing that the order of cells may vary so simply filtering the output of the above code based on the order may not work. I think the solution I need has to be contained within the original awk line and tied to a unique identifier like Address. Any tips are much appreciatted. What an amazing flexibility in Linux. I wish I had more time to play. |
Thanx for the oneliner, osor, the best approach I've found so far.
But, in my case, it outputs something like: Code:
2.437 19/70 off Code:
iwlist ath0 scanning | awk -F '[ :=]+' '/Ad/{printf substr($0,30)" "} /(Channel:|Qual)/{printf $3" "} /Encr/{printf $4" "} /ESS/{print substr($0,27)}' |
All times are GMT -5. The time now is 06:24 AM. |