Skip to content

Instantly share code, notes, and snippets.

@billy3321
Created April 10, 2012 10:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save billy3321/2350123 to your computer and use it in GitHub Desktop.
Save billy3321/2350123 to your computer and use it in GitHub Desktop.
This script is used to connect to wify
#!/bin/bash
# This script is writen by billy3321
# this script is used to connect to wireless network.
#set -o xtrace
IF=$(echo $1 | tr 'A-Z' 'a-z')
AUTH=$(echo $2 | tr 'A-Z' 'a-z')
ESSID=$3
KEY=$4
function help(){
#This is print function, use to print help.
echo "usage:"
echo ' connect_wify.sh -h : print this help'
echo ' connect_wify.sh $IF $AUTH $ESSID $KEY'
echo ' $AUTH shoud be in none|wep|wpa|wpa2'
echo "example:"
echo " connect_wify.sh wlan0 wpa dlink 1234567890"
}
function initif(){
#Init the interface
sudo ifconfig $IF down
sudo iwconfig $IF mode managed
sudo ifconfig $IF up
sleep 1
#Try to scan the essid for test the wireless card
sudo iwlist $IF scan
if [ $? -ne 0 ]; then
echo "the wireless card cannot work."
exit
fi
sleep 1
}
if [ -z $IF ] || [ $IF = "help" ] || [ $IF = "-h" ] || [ $IF = "--help" ]; then
help
exit
else
read -p "press ENTER to continue, Ctrl-C to exit." INPUT
fi
case "$AUTH" in
"none")
#The essid don't use key
initif
sudo iwconfig $IF essid $ESSID
;;
"wep")
#The essid use wep key.
initif
#hex key
sudo iwconfig $IF essid $ESSID key $KEY
#ascii key
#sudo iwconfig $IF essid $ESSID key s:${KEY}
;;
"wpa")
initif
#First way to connect
sudo iwconfig $IF essid $ESSID
sudo iwpriv $IF set AuthMode=WPAPSK
sudo iwpriv $IF set EncrypType=TKIP
sudo iwpriv $IF set WPAPSK=$KEY
#Second way to connect
#wpa_passphrase $ESSID $KEY > /tmp/wpa.conf
#sudo wpa_supplicant -BDwext -iwlan0 -c/tmp/wpa.conf
;;
"wpa2")
initif
#First way to connect
sudo iwconfig $IF essid $ESSID
sudo iwpriv $IF set AuthMode=WPA2PSK
sudo iwpriv $IF set EncrypType=TKIP
sudo iwpriv $IF set WPA2PSK=$KEY
#Second way to connect
#wpa_passphrase $ESSID $KEY > /tmp/wpa.conf
#sudo wpa_supplicant -BDwext -iwlan0 -c/tmp/wpa.conf
;;
*)
echo "auth mode should in none, wep, wpa, wpa2"
help
;;
esac
#Get the ip through dhcp
sleep 1
sudo dhclient -v $IF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment