You are not logged in.

#1 2011-11-28 05:04:14

songandsilence
Member
From: Burlingame, KS, USA
Registered: 2010-12-01
Posts: 28

wifi-connect

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

#2 2011-11-28 06:26:51

dolik.rce
Member
From: Czech republic
Registered: 2011-05-04
Posts: 43

Re: wifi-connect

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 wink

Offline

#3 2011-11-30 18:12:50

songandsilence
Member
From: Burlingame, KS, USA
Registered: 2010-12-01
Posts: 28

Re: wifi-connect

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

Board footer

Powered by FluxBB