LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices



Reply
 
Search this Thread
Old 12-01-2009, 06:58 AM   #1
urban.yoga.journeys
Member
 
Registered: Jun 2008
Posts: 31

Rep: Reputation: 16
wifi connect script (tested in UBUNTU)


i'm not sure if this is the correct place to put this in. i wrote a script for connecting to a wifi network. it allows for WPA encryption, though i've only tested it with WPA 1. it doesn't allow for ad-hoc systems, since i've never managed to manually connect to one.

if people had some free time to take a look at it and maybe clean it up, or clear up the features it would be great. it's not the cleanest code, partly because i figured out some stuff along the way, and partly because i'm still not very familiar with bash syntax and all the available tools.

oh, for the WPA to work, since the script will configure the settings without WPA supplicant, you'll have to make the following changes in the /etc/NetworkManager//nm-system-settings.conf (if you're using network manager)

Code:
[ifupdown]
managed=false
anyway here it is. let me know what you guys think!

Code:
#!/bin/bash

#removing possible previous temp file
rm list.temp 2>/dev/null

#scans for wifi connections & isolates wifi AP name
# thanks to jonjgc for the array solution
#thanks to ghostdog74 for the AWK suggestion

eval list=( $(sudo iwlist scan 2>/dev/null | awk -F":" '/ESSID/{print $2}') )

#sets prompt
PS3="Choose wifi connection: "

#tests for number of wifi connections, exits if none
if [ -z "${list[0]}" ]; then
	clear
	echo "No available wifi connection"
	exit 1
fi

#menu of wifi connections
select item in "${list[@]}"; do

#sets essid as value for WIFI variable and displays information about the AP
	wifi=$(echo $item)
        sudo iwlist scan 2>/dev/null | sed -n "/$wifi/, +9p" > list.temp
	echo "$(cat list.temp | sed 's/^[ \t]*//')"

#sets channel as value for CHANNEL variable
	channel=$(grep Channel: list.temp | sed 's/.*Channel://g')

#test for mode, if mode = master, sets MODE variable to managed
	mode=$(grep Mode list.temp | sed 's/.*Mode://g')
	if [ "$mode" == "Master" ]; then
		mode="managed"
	else
		clear
		echo "Cannot connect"
		exit
	fi

#tests for encryption key
	key=$(grep key: list.temp | sed 's/.*key://g')
	if [ $key == "on" ]; then
		echo -n "Enter encryption key: "
		read key
	fi

#checks encryption algorithm
	IE=$(grep IE list.temp | sed 's/^ .*IE: \(...\).*/\1/')

#writes to /etc/network/interfaces file for WPA encryption: essid, key, protocols, etc.
	if [ "$IE" == "WPA" ]; then
		sudo cp /etc/network/interfaces /etc/network/interfaces.bakup
		sudo sed -i 's/iface wlan0 inet manual/iface wlan0 inet dhcp/' /etc/network/interfaces
		sudo sed -i -e "/dhcp/a\wpa-passphrase $key" \
	-e "/dhcp/a\wpa-driver wext" \
	-e "/dhcp/a\wpa-key-mgmt WPA-PSK" \
	-e "/dhcp/a\wpa-proto WPA" \
	-e "/dhcp/a\wpa-ssid \"$wifi\"" /etc/network/interfaces
	sudo /etc/init.d/networking restart
	sudo cp /etc/network/interfaces.bakup /etc/network/interfaces
	sudo rm /etc/network/interfaces.bakup
	exit

	else

#sets the wireless configuration for non WPA: essid, channel, mode, key, etc
		sudo iwconfig wlan0 essid \""$wifi"\" channel $channel mode $mode key $key
		echo "------------------------------------------------"
		echo "Connecting to: $wifi at channel: $channel, mode: $mode"
		echo "------------------------------------------------"

#connects to wifi connection
		sudo dhclient
		exit
	fi
done

Last edited by urban.yoga.journeys; 12-06-2009 at 03:36 AM.
 
Old 12-01-2009, 10:25 AM   #2
TB0ne
Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 15,087

Rep: Reputation: 2714Reputation: 2714Reputation: 2714Reputation: 2714Reputation: 2714Reputation: 2714Reputation: 2714Reputation: 2714Reputation: 2714Reputation: 2714Reputation: 2714
Quote:
Originally Posted by urban.yoga.journeys View Post
i'm not sure if this is the correct place to put this in. i wrote a script for connecting to a wifi network. it allows for WPA encryption, though i've only tested it with WPA 1. it doesn't allow for ad-hoc systems, since i've never managed to manually connect to one.

if people had some free time to take a look at it and maybe clean it up, or clear up the features it would be great. it's not the cleanest code, partly because i figured out some stuff along the way, and partly because i'm still not very familiar with bash syntax and all the available tools.

oh, for the WPA to work, since the script will configure the settings without WPA supplicant, you'll have to make the following changes in the /etc/NetworkManager//nm-system-settings.conf (if you're using network manager)
Nice, thanks for sharing it. You might want to post things like this in "Success Stories", too....
 
Old 12-01-2009, 08:46 PM   #3
urban.yoga.journeys
Member
 
Registered: Jun 2008
Posts: 31

Original Poster
Rep: Reputation: 16
cool, will post there in the future. is there a particular site on the web where people post scripts for further development/bug testing from the community?
 
Old 12-01-2009, 10:11 PM   #4
jonjgc
LQ Newbie
 
Registered: Nov 2009
Posts: 17

Rep: Reputation: 6
Your collection of network ESSID's could go into an array and proper expansion
of the array values in the select statement could deal with spaces in the names.

list=( $(sudo iwlist scan 2>/dev/null |grep ESSID | sed 's/.*ESSID:"\(.*\)".*/\1/') )

...

select item in "${list[@]}"
do
...
done
 
Old 12-02-2009, 02:47 AM   #5
urban.yoga.journeys
Member
 
Registered: Jun 2008
Posts: 31

Original Poster
Rep: Reputation: 16
hey didn't think of that, much better way.
 
Old 12-02-2009, 04:10 AM   #6
urban.yoga.journeys
Member
 
Registered: Jun 2008
Posts: 31

Original Poster
Rep: Reputation: 16
tried out the array on a test script and the output was perfect:

Code:
#!/bin/bash

list=("one" "two three" "four")
select item in "${list[@]}"; do
	echo $item
done

mo@mo-laptop:~/bin$ ./tester.sh
1) one
2) two three
3) four
however when i tried it on the main script any spaces in the names would still screw up the output. i think the issue is in populating the array.

Code:
list=( $(sudo iwlist scan 2>/dev/null |grep ESSID | sed 's/.*ESSID:"\(.*\)".*/\1/') )
i've tried playing around with the quotation marks, but haven't managed to make it run correctly yet.

what i've tried:

list=( $(sudo iwlist scan 2>/dev/null |grep ESSID | sed 's/.*ESSID:\(".*"\).*/\1/') )

list=( "$(sudo iwlist scan 2>/dev/null |grep ESSID | sed 's/.*ESSID:"\(.*\)".*/\1/')" )

Last edited by urban.yoga.journeys; 12-02-2009 at 04:11 AM.
 
Old 12-03-2009, 01:48 AM   #7
jonjgc
LQ Newbie
 
Registered: Nov 2009
Posts: 17

Rep: Reputation: 6
I don't have a wireless system available to play with right now.
It looks to me like this one "should" do the trick:

list=( $(sudo iwlist scan 2>/dev/null |grep ESSID | sed 's/.*ESSID:\(".*"\).*/\1/') )

but you said it did not run correctly. This should also be correct, but shouldn't be
different than the above line:

list=( $(sudo iwlist scan 2>/dev/null |grep ESSID | sed 's/.*ESSID:"\(.*\)".*/"\1"/') )

What is the output of the pipeline between the $( and ). If my memory serves, it should be
a set of quoted ssid, one per line. And if that is the case, the assignment line that is
created would be something like:

list=( "ssidOne"
"SecondSSID"
"SSID with spaces"
"ssid_four" )

which should be ok. Another thing to check is what is assigned to list.
A simple loop should do it:

for ((i=0; i < ${#list[@]}; i++)
do
echo $i ${list[i]}
done
 
Old 12-03-2009, 11:06 PM   #8
urban.yoga.journeys
Member
 
Registered: Jun 2008
Posts: 31

Original Poster
Rep: Reputation: 16
already ran that test.

Code:
echo ${list[*]}

"GLOBE TATTOO WIFI ZONE" "MDvismin" "PSG" "KMClahug"
but the output of select is:
Code:
1 ) "GLOBE
2 ) TATTOO
3 ) WIFI
4 ) ZONE"
5 ) "MDvismin"
6 ) "PSG"
7 ) "KMClahug"
might have to set the IFS and see if that will do the trick.
 
Old 12-04-2009, 01:08 AM   #9
jonjgc
LQ Newbie
 
Registered: Nov 2009
Posts: 17

Rep: Reputation: 6
AHH, that explains it.

Note the double quotes are being assigned to list[x] rather than quoting the strings.
A shell line is evaluated in a series of discrete steps, one of them being looking
for quoting. These quotes are showing up after that step is completed so the quotes
are just taken as text.

What you need to do is force the shell to re-evaluate the line.

Put the word "eval" in front of list=(...), i.e.

eval list=( $(...) )
 
1 members found this post helpful.
Old 12-04-2009, 02:14 AM   #10
urban.yoga.journeys
Member
 
Registered: Jun 2008
Posts: 31

Original Poster
Rep: Reputation: 16
great, that did the trick. so eval allows the script to any special characters that are not hard coded to be read as special character instead of text? sort of like echo -e allows for backslash escapes?
 
Old 01-02-2010, 01:33 AM   #11
linuxhelpneeded
LQ Newbie
 
Registered: Jan 2010
Posts: 2

Rep: Reputation: 0
ok so I'm running ubuntu 7.1 on my ps3 how exactly do I put this to use?
here are the outputs of ifconfig

Link encap:Ethernet HWaddr 00:19:C5:7f:C6:74
UP BROADCAST RUNNING MULITCAST MTU:1500 METRIC:1
RX PACKETS:0 errors :0 dopped: 0 overruns: 0 frame:0
TX packets:42 errors:0 dropped:21 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytex:0 (0.0 b) TX bytes: 7808 (7.6 KB)
Interrupt:54

Eth0: avah

Link encap:Ethernet HWaddr 00:19:C5:7F:C6:74C
inet addr:169.254.8.125 Bcast:169.254.255.255 Mask:255.255.0.0
UP BROADCAST RUNNING MULTICAST MTU:150 Metric:1
Interrupt:54

lo

Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets: 172 errors:0 dopped:0 overruns:0 frame:0
TX packets:172 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueulen:0
RX bytes:12788 (12.4 KB) TX bytes:12788 (12.4 KB)

iwconfig yeilds

lo no wireless extensions

eth0 no wireless extensions
 
Old 01-03-2010, 04:24 PM   #12
urban.yoga.journeys
Member
 
Registered: Jun 2008
Posts: 31

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by linuxhelpneeded View Post
ok so I'm running ubuntu 7.1 on my ps3 how exactly do I put this to use?
here are the outputs of ifconfig

Link encap:Ethernet HWaddr 00:19:C5:7f:C6:74
UP BROADCAST RUNNING MULITCAST MTU:1500 METRIC:1
RX PACKETS:0 errors :0 dopped: 0 overruns: 0 frame:0
TX packets:42 errors:0 dropped:21 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytex:0 (0.0 b) TX bytes: 7808 (7.6 KB)
Interrupt:54

Eth0: avah

Link encap:Ethernet HWaddr 00:19:C5:7F:C6:74C
inet addr:169.254.8.125 Bcast:169.254.255.255 Mask:255.255.0.0
UP BROADCAST RUNNING MULTICAST MTU:150 Metric:1
Interrupt:54

lo

Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets: 172 errors:0 dopped:0 overruns:0 frame:0
TX packets:172 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueulen:0
RX bytes:12788 (12.4 KB) TX bytes:12788 (12.4 KB)

iwconfig yeilds

lo no wireless extensions

eth0 no wireless extensions
you have to modify the script to whatever your interface is. the script default is wlan0 because that's what my system uses.

iwconfig only yields lo and eth0? nothing else? how about iwlist scan?
 
  


Reply

Tags
iwconfig, wifi, wireless


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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Ubuntu 8.10 cannot connect to WPA WiFi network Wiseloki Linux - Wireless Networking 2 12-29-2009 04:31 AM
Wifi won't connect with ubuntu 9.10 Rockgod1969 Linux - Software 3 11-30-2009 01:57 AM
Ubuntu 9.04 can't connect to wifi with Intel Wireless WiFi Link 5100 Shinbatsu Linux - Networking 3 08-30-2009 01:55 AM
script to connect to wifi with various configurations dgallig Slackware 3 07-03-2008 07:10 AM
ubuntu 6.6 wifi shows but can't connect buckjams Ubuntu 3 02-14-2007 10:59 PM


All times are GMT -5. The time now is 04:18 PM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration