You are not logged in.
Pages: 1
I'm writing a script to connect to wifi networks easily. I'm not exactly the greatest at shell scripting, so this is probably loaded with all kinds of errors.
Bugs? Comments? Suggestions? Pats on the back?
#!/bin/bash
#
# wifi-connect: A Bash script to easily connect to a wireless
# network via the command line.
#
# Please report any suggestions, or bugs to James Miller.
# <songandsilence27@gmail.com>
#
# Exit statuses:
# 1: General/Unknown Error
# 2: Not Root
# 3: Can't DHCP
# 4: Can't Ping
#
# Check and see if this is the root user, or at least has root privileges.
#
if [$suid != 0]
echo "You need to be root to run this script"
then exit 2
fi
# Here, we gather some basic information about the wireless network and the user's card.
read -p "What is the name of the wireless interface you are using? (examples: wlan0)" wlanif
read -p "What is the ESSID (Network Name) of the wireless network using?" essid
read -p "Which encryption is the wireless network using? (none,wep-hex,wep-pass,wpa)" encrypt
case "$encrypt" in
none)
iwconfig wlan0 essid "$essid"
;;
wep-hex) read -p "What is the encryption key?" wephexkey
iwconfig essid $essid key $wpahexkey $wlanif
;;
wep-pass) read -p "What is the encryption key?" weppasskey
iwconfig essid $essid key s:$wpahexkey $wlanif
;;
wpa) read -p "What is the encryption key?" wpakey
wpa_passphrase $essid "$wpakey" > /etc/wpa_supplicant.conf
wpa_supplicant -B -Dwext -i $wlanif -c /etc/wpa_supplicant.conf
;;
*) echo "Please select a valid encyption method. (none,wep-hex,wep-pass,wpa)"
# this ;; is optional as it's the last one
;;
esac
# Here, we check and see if dhcpcd is running, and if it is, kill it,
# then restart it. I'm sure there's a better way to do this than the
# check-kill-restart method.
if [ -z "$(pgrep dhcpcd)" ]
then
pkill dhcpcd
dhcpcd $wlanif
else
dhcpcd $wlanif
elif exit 3
# Finally, we ping google to test the connection.
if ping -c 3 www.google.com != /dev/null
then echo "Success! You are connected to the wireless network $essid!"
else echo "Sorry, you are not connected to the wireless network."
elif exit 4
fi
# Aaaaand, we're done. May the source be with you.
exit
Offline
Have you seen wifi-select? https://wiki.archlinux.org/index.php/Netcfg#wifi-select
It does all the things your script does and many more
Offline
I actually use wicd, I was just writing this for fun. But, I will check out wifi-select, and see where I can improve upon it.
Offline
Pages: 1