Advertisement
Dj_Dexter

connectWifi

Sep 24th, 2013
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.13 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Revisited and fixed by Dj_Dexter 2013-09-24
  4. # re tested in slackware 14.0 and runs fine :)
  5. # only works for wep encripted connections
  6.  
  7. # die with honor
  8. function usage() {
  9.     echo "usage: ./connectWifi \"<essid>\" [key]"
  10.     echo "       *key in ascii prefaces s:[key]"
  11.     echo "Channel number of channel"
  12.         echo "Example connectWifi "essid" s:key 3"
  13.     exit 1
  14. }
  15.  
  16. # killed in disgrace
  17. function die() {
  18.     echo
  19.     echo "   [*] ERROR: $1..."
  20.     echo
  21.     exit 1
  22. }
  23.  
  24. # check for essid
  25. if [[ $1 ]]; then
  26.     # setup
  27.     echo "Starting..."
  28.     ifconfig wlan0 down
  29.     dhclient -r wlan0 -q
  30.     echo " -Previous connection dropped."
  31.     iwconfig wlan0 essid "$1"
  32.     echo " -ESSID Set to $1."
  33.     # check for key
  34.     if [[ $2 ]]; then
  35.         iwconfig wlan0 key $2
  36.         echo " -Key set to $2."
  37.     fi
  38.     # connect
  39.         iwconfig wlan0 channel "$3"
  40.         ifconfig wlan0 up
  41.         iwconfig wlan0 mode Managed
  42.     echo " -Connecting..."
  43.     echo
  44.     dhclient wlan0
  45.     # announce success
  46.     if [[ $? -eq 0 ]]; then
  47.         echo
  48.         echo "Successfully connected to $1!"
  49.         echo
  50.         exit 0
  51.     fi
  52.  
  53. else
  54.     # no variables
  55.     usage
  56. fi
  57.  
  58. # declare failure
  59. die "Failed to connect to $1."
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement