Bash script for wireless

January 03rd, 2006 - 04:20 am ET by John OHagan | Report spam
Hi,

First, sorry if you've read this already, but I mis-posted it in an existing
thread instead of as a new one. Here's my question:

Calling all scripting experts! I'm trying to write a script which will present
a list of available wireless networks as a menu, from which the user can
select one to connect to.

I want to take selected parts of the output of `iwlist scan` as variables, but
have been unable to find out how to do this in online scripting guides. I
know how to use grep etc. to find known bits of output, but not how to
isolate arbitrary output, for example ESSID names.

Can anyone point me in the right direction?

Thanks,

John


To UNSUBSCRIBE, email to debian-laptop-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
email Follow the discussionReplies 12 repliesReplies Make a reply

Similar topics

Replies

#1 Vlastimil Pavicek
January 03rd, 2006 - 07:10 am ET | Report spam
Hi,

First, sorry if you've read this already, but I mis-posted it in an existing
thread instead of as a new one. Here's my question:

Calling all scripting experts! I'm trying to write a script which will present
a list of available wireless networks as a menu, from which the user can
select one to connect to.

I want to take selected parts of the output of `iwlist scan` as variables, but
have been unable to find out how to do this in online scripting guides. I
know how to use grep etc. to find known bits of output, but not how to
isolate arbitrary output, for example ESSID names.



I am no expert, but

iwlist eth1 scan | grep -o 'ESSID\"[^\"]*\"' | while read f ; do echo "{f:6}" ; done

does something, maybe this could be fed into an array and accessed from it

read section "Parameter Expansion" in bash manual page, sure you will find better solution there

maybe awk could be better tool for this, but again, I am no expert...


HAllellujAH
VLP


To UNSUBSCRIBE, email to
with a subject of "unsubscribe". Trouble? Contact
Replies Reply to this message
#2 Vlastimil Pavicek
January 03rd, 2006 - 07:10 am ET | Report spam
Hi,

First, sorry if you've read this already, but I mis-posted it in an existing
thread instead of as a new one. Here's my question:

Calling all scripting experts! I'm trying to write a script which will present
a list of available wireless networks as a menu, from which the user can
select one to connect to.

I want to take selected parts of the output of `iwlist scan` as variables, but
have been unable to find out how to do this in online scripting guides. I
know how to use grep etc. to find known bits of output, but not how to
isolate arbitrary output, for example ESSID names.



I am no expert, but

iwlist eth1 scan | grep -o 'ESSID\"[^\"]*\"' | while read f ; do echo "{f:6}" ; done

does something, maybe this could be fed into an array and accessed from it

read section "Parameter Expansion" in bash manual page, sure you will find better solution there

maybe awk could be better tool for this, but again, I am no expert...


HAllellujAH
VLP


To UNSUBSCRIBE, email to
with a subject of "unsubscribe". Trouble? Contact
Replies Reply to this message
#3 Johannes Zarl
January 03rd, 2006 - 10:50 am ET | Report spam
Hi John,

I know how to use grep etc. to find known bits of output, but not
how to isolate arbitrary output, for example ESSID names.

Can anyone point me in the right direction?



I used this in one script of mine:

available_essids=`iwlist air0 scanning | grep "ESSID" | sed 's/.*"\(.*\)".*/\1/'`

As so often, sed is write-once-read-never, so I will try to explain;-)

After the grep the input to sed should look something like this:
<output of grep>
ESSID:"FOO"
ESSID:"BAR"
ESSID:"BAZ"
</output of grep>

sed will now extract the string inside the quotes:
<part1 '.*"> ESSID:"</part1><grouped part1 '\(.*\)'>FOO</grouped part1><ungrouped part2 '".*'>"</ungrouped part2>
etc.

Well, maybe this is even more obfuscated, but at least I tried..

Cheers,
Johannes


To UNSUBSCRIBE, email to
with a subject of "unsubscribe". Trouble? Contact
Replies Reply to this message
#4 Johannes Zarl
January 03rd, 2006 - 10:50 am ET | Report spam
Hi John,

I know how to use grep etc. to find known bits of output, but not
how to isolate arbitrary output, for example ESSID names.

Can anyone point me in the right direction?



I used this in one script of mine:

available_essids=`iwlist air0 scanning | grep "ESSID" | sed 's/.*"\(.*\)".*/\1/'`

As so often, sed is write-once-read-never, so I will try to explain;-)

After the grep the input to sed should look something like this:
<output of grep>
ESSID:"FOO"
ESSID:"BAR"
ESSID:"BAZ"
</output of grep>

sed will now extract the string inside the quotes:
<part1 '.*"> ESSID:"</part1><grouped part1 '\(.*\)'>FOO</grouped part1><ungrouped part2 '".*'>"</ungrouped part2>
etc.

Well, maybe this is even more obfuscated, but at least I tried..

Cheers,
Johannes


To UNSUBSCRIBE, email to
with a subject of "unsubscribe". Trouble? Contact
Replies Reply to this message
#5 John OHagan
January 05th, 2006 - 09:00 am ET | Report spam

On Wed, 4 Jan 2006 02:26 am, Johannes Zarl wrote:


> I know how to use grep etc. to find known bits of output, but not
> how to isolate arbitrary output, for example ESSID names.
>
> Can anyone point me in the right direction?

I used this in one script of mine:

available_essids=`iwlist air0 scanning | grep "ESSID" | sed
's/.*"\(.*\)".*/\1/'`




[...]


Thanks to everyone who replied; it is all good advice. The above method caused
ESSIDs with spaces to become separate strings, which caused malfunctions when
the result was fed into iwconfig. In the end I used gawk with ":" as a field
separator to extract them:

networks=$(iwlist eth1 scan | grep ESSID | gawk -F: '{ print $2 }')

then using `select`, fed the result into iwconfig, which doesn't seem to mind
the quotes being preserved around the ESSID names.

I put the script into Kmenu (with "Run in terminal" and "Run as different
user" checked), and now I can quickly choose and connect to any available
wireless networks I happen to find while roaming. Just what I always wanted!

Thanks again,

John

P.s. The script is attached - it assumes your wireless interface is called
eth1.


name="wifind"
filename="wifind"

#!/bin/bash

test=(`iwlist eth1 scan | grep "No scan results"`)

if [ $test > 0 ] ; then
echo
echo No networks found
echo
sleep 3
exit
fi

networks=$(iwlist eth1 scan | grep ESSID | gawk -F: '{ print $2 }')

echo
iwlist eth1 scan
echo

echo
echo Enter the number of the network you want to connect to:
echo

select network in $networks ; do

echo
echo Connecting to $network
echo

iwconfig eth1 ESSID $network

sleep 3

exit

done












To UNSUBSCRIBE, email to
with a subject of "unsubscribe". Trouble? Contact
Replies Reply to this message
Help Create a new topicNext page Replies Make a reply
Search Make your own search