You are not logged in.
I got sick of overcomplicated network management tools, so I wrote an extremely minimal script for manually connecting to wifi networks (ones that use DHCP anyway). Here it is, in all its glory:
#!/bin/sh
#
# wiconnect - a script for connecting to arbitrary wifi networks
# Usage: wiconnect [-open | -wep | -wpa ] iface essid
#
if [ $USER != "root" ]; then
exit "$0 must be run as root. You should probably use sudo."
fi
PATH=/usr/sbin:/sbin:/usr/bin:/bin
DHCLIENT=$(command -v dhcpcd dhclient | tail -n 1) ||
exit "No DHCP client detected, please install one."
get_passphrase() {
stty -echo
IFS= read -p 'Passphrase: ' -r PASS
stty echo
echo $PASS
}
# Some housekeeping
ifconfig $2 up
kill $(pidof wpa_supplicant)
kill -9 $(pidof wpa_supplicant)
if [ $1 == "-open" ]; then
echo "Connecting to insecure open network $3..."
iwconfig $2 essid $3
$DHCLIENT $2
elif [ $1 == "-wep" ]; then
echo "Connecting to insecure WEP network $3..."
PASS=$(get_passphrase)
iwconfig $2 essid $3 key $PASS
$DHCLIENT $2
elif [ $1 == "-wpa" ]; then
echo "Connecting to possibly secure WPA network $3..."
PASS=$(get_passphrase)
iwconfig $2 essid $3
wpa_passphrase $3 $PASS | wpa_supplicant -i$2 -Dwext -c/dev/stdin -B
$DHCLIENT $2
else
echo "Usage: $0 [-open | -wep | -wpa ] iface essid"
fi
The idea is you put it somewhere in PATH, run 'sudo wiconnect blah blah $essid', and it connects you. The problem is... well, there are several, in order of decreasing magnitude.
1. I don't know if it works at all, because I don't have access to any wifi networks at the moment. My suspicion is that WPA connections will turn out to require a delay so that wpa_supplicant has time to associate.
2. Error handling is obviously not that good! I will probably post updates as problems (and hopefully solutions) become apparent.
3. I know there are other DHCP clients that I should check for, but I don't know which ones, or which are preferable.
4. On a related note, 'which' whines when something can't be found, and I don't know how to shut it up. I tried redirecting STDOUT and STDERR output to /dev/null, but somehow that doesn't work.
Anyway I'm interested in feedback so I can make this (more) useful. If you can spot any obvious, glaring issues in it, please do say!
Edit: updated with rockin turtle's and altercation's suggestions.
Last edited by Gullible Jones (2013-02-05 18:59:46)
Offline
I got here after reading your "Here, have a GUI" post, which I quite liked, though I didn't feel the ensuing thread really addressed your points. Anyhow, I'm 100% in agreement with that post and also with simple tools such as this script.
FWIW, I'd use `command -v` instead of `which`. It should be less verbose on error (though will still error out). If you provide multiple arguments to it (e.g. `command -v name1 name2`) it will only error out if none of them are found, so you don't need tail in that case.
Ethan Schoonover
Precision Colors - http://ethanschoonover.com/solarized
Offline
A few small points.
First, I prefer to use bash over sh. Unless you specifically need 'sh' to support some platform, it's usually easier to just put
#!/bin/bash
at the top.
You could make DHCLIENT be an array containing the available dhcp clients by putting () around your $(which ...) command.
DHCLIENT=($(which dhcpcd dhclient badclient 2>/dev/null))
if [ ${#DHCLIENT[@]} -eq 0 ]; then
echo "No DHCP client detected, please install one."
exit
fi
The first client it finds will be in ${DHCLIENT[0]}. The 2>/dev/null at the end will cause any clients it doesn't find to be discarded.
Sometimes a person may use your script, but may name it something other than 'wiconnect'. Perhaps you should do:
if [ $USER != "root" ]; then
echo "$0 must be run as root. You should probably use sudo."
exit
fi
...
echo "Usage: $0 [-open | -wep | -wpa ] iface essid"
the $0 will be substituted with the name of the script that was actually invoked.
The -9 in 'pkill -9 wpa_supplicant' is also frowned upon as it doesn't allow wpa_supplicant to clean itself up before it shuts down. You could do something like
pkill wpa_supplicant
pid=$(pgrep wpa_supplicant) && kill -9 $pid
to give it a chance first.
Offline
Yay responses! Thank you, I will update the script following your suggestions.
Edit: re using bash, I was hoping to keep it portable enough to use on *BSD (at least with minimal modification). However, I currently don't use BSD for anything; and anyway those OSes seem to be dying off on the desktop, which is IMO very sad, because they offer nice features out of the box that Linux doesn't. [/OT]
Edit 2: not sure how to avoid using tail?
Edit 3: and updated.
Last edited by Gullible Jones (2012-12-01 20:45:51)
Offline
You can avoid using tail by making DHCLIENT be an array. Each client that you find will then be an element in the array. You make an array in bash by putting () around the command.
DHCLIENT=($(which dhcpcd dhclient 2>/dev/null))
as opposed to
DHCLIENT=$(which dhcpcd dhclient 2>/dev/null)
Note the extra level of parenthesis.
Now, if both dhcpcd and dhclient are on your system, then
${DHCLIENT[0]} will contain /usr/bin/dhcpdc
${DHCLIENT[1]} will contain /usr/bin/dhclient
If only one of the two are installed, then it will be in ${DHCLIENT[0]} and ${DHCLIENT[1]} will be unset.
Offline
Edit 2: not sure how to avoid using tail?
Forget it, i was apparently high on bash when I wrote that. You *can* skip tail if you want to use something like `expr` to strip out a regex match but, while it's not significantly longer than using tail, I'd say that it's less clear, and expr isn't a builtin so you aren't saving anything there.
EDIT: rockin turtle has an elegant solution above. That rocky mountain air obviously enhances clarity of thought
Last edited by altercation (2012-12-02 00:09:49)
Ethan Schoonover
Precision Colors - http://ethanschoonover.com/solarized
Offline
A very belated update: after doing some long overdue reading on shell scripting, I think I can safely say that you should not use this script. Seriously. With sudo privilges on it, a malicious party could probably get root in two seconds.
My bad, people. I'll try to make it more sensible later this evening.
Offline