Linux - GeneralThis 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
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.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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 02:36 AM.
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....
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?
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.
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.
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
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.
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?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.