Advertisement
Guest User

connect.sh

a guest
Jun 1st, 2013
808
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 5.01 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. #
  4. # 01/06/2013
  5. # This script attempts to semi-automate the wifi connection process from the command line.
  6. # It is intended to be used on a headless machine without the requirement of typing several commands for a connection.
  7. # The script stores previous connection credentials in PLAINTEXT as *.wpa files in the executed directory and in /etc/wpasupplicant.conf.  These .wpa files are used to connect to several different ap using  previously stored info.
  8. # Probably a good idea to stop and other network managing software while running this script, also in testing wpa_supplicant does a pretty good job of re-connecting a disassociated link automatically.
  9. #
  10. # Mainly created from a combination of scripts taken from theses two sources:
  11. # http://www.backtrack-linux.org/forums/archive/index.php/t-3367.html
  12. # AND
  13. # http://www.linuxquestions.org/questions/linux-general-1/wifi-connect-script-tested-in-ubuntu-772646/
  14. #
  15. # Copy, Distribute and Modify Freely.
  16. #
  17.  
  18.  
  19. INT=$1
  20.  
  21. if [ -z "$1" ]; then
  22.     printf "Usage: $0 [interface]\n"
  23.     exit
  24. fi
  25.  
  26. if [ "$(id -u)" != "0" ]; then
  27.     printf "This script must be run as root\n" 1>&2
  28.     exit
  29. fi
  30.  
  31. #
  32. # Search for previous saved config files
  33. #
  34. function read_saved {
  35.     #
  36.     # Search for previous wpa configuration files.
  37.     #
  38.  
  39.     #
  40.     # Save and change IFS so spaces in file names are not interpreted as separate lines in the array
  41.     #
  42.     OLDIFS=$IFS
  43.     IFS=$'\n'
  44.  
  45.     #
  46.     # Read all file names into an array ref:http://www.cyberciti.biz/tips/handling-filenames-with-spaces-in-bash.html
  47.     # " -printf '%f\n' " removes path info from outputs ref:http://serverfault.com/questions/354403/remove-path-from-find-command-output
  48.     #
  49.     SAVED_LIST=($(find . -type f -name "*.wpa" -printf '%f\n'))
  50.  
  51.     #
  52.     # restore ifs
  53.     #
  54.     IFS=$OLDIFS
  55.  
  56.  
  57.     #
  58.     # Tests for number of saved wifi connections, if none exit
  59.     #
  60.     if [ -z "${SAVED_LIST[0]}" ]; then
  61.         printf "There are no previous saved wifi connections\n\n"
  62.         #
  63.         # Create new connection
  64.         #
  65.         conf_create
  66.     fi
  67.  
  68.     #
  69.     #PS3 Sets the prompt for the select statement below
  70.     #
  71.     PS3="Choose a previously saved wifi connection or 's' to skip: "
  72.  
  73. #
  74. #Select one of the previous saved configurations to connect with or quit
  75. #
  76. select ITEM in "${SAVED_LIST[@]}"; do
  77.     #
  78.     # Quit if selected number does not exist or alpha in entered
  79.     #
  80.     if [ -z "$ITEM" ] ; then
  81.             printf "Skipping\n"
  82.             conf_create
  83.     fi
  84.  
  85.     printf "$ITEM is selected\n"
  86.     cat "$ITEM">/etc/wpa_supplicant.conf | xargs
  87.     connect "$ITEM"
  88. done
  89. }
  90.  
  91. function conf_create (){
  92.     #
  93.     # Scans for wifi connections & isolates wifi AP name
  94.     #
  95.     eval LIST=( $(sudo iwlist $INT scan 2>/dev/null | awk -F":" '/ESSID/{print $2}') )
  96.  
  97.     #
  98.     #PS3 Sets the prompt for the select statement below
  99.     #
  100.     PS3="Choose wifi connection or 'q' to quit: "
  101.  
  102.     #
  103.     # Tests for number of wifi connections, exits if none
  104.     #
  105.         if [ -z "${LIST[0]}" ]; then
  106.             printf "No available wifi connection using $INT\n"
  107.             exit
  108.         fi
  109.  
  110.     #
  111.     # Select from a LIST of scanned connections
  112.     #
  113.     select ITEM in "${LIST[@]}"; do
  114.     ifconfig $INT | grep inet
  115.  
  116.         #
  117.         # Quit if selected number does not exist or alpha in entered
  118.         #
  119.         if [ -z "$ITEM" ] ; then
  120.                 printf "Exiting\n"
  121.                 exit
  122.         fi
  123.  
  124.         #
  125.         # Get user input for passphrase no need to escape spaces
  126.         #
  127.         printf "Enter the passphrase for $ITEM?\n"
  128.         read "PASSPHRASE"
  129.  
  130.         #
  131.         # Append the ITEM variable (ESSID) to .wpa to make a filename for saved configs
  132.         #
  133.         FILENAME=$ITEM".wpa"
  134.  
  135.         #
  136.         # Run wpa_passphrase to generate a file for wpa_supplicant to use, store it locally and in etc/wpa_supplicant.conf
  137.         #
  138.         printf "Running wpa_passphrase\n"
  139.         wpa_passphrase "$ITEM" "$PASSPHRASE" > "$FILENAME" | xargs
  140.         cat "$FILENAME">/etc/wpa_supplicant.conf | xargs
  141.         printf "Creating new configuration using $ITEM\n"
  142.  
  143.         #
  144.         # Jump to connect function, pass on the ESSID variable for connection
  145.         #
  146.         connect $ITEM
  147.     done
  148. }
  149.  
  150. function connect (){
  151.     printf "Connecting using file $*\n"
  152.  
  153.     #
  154.     # Capture incoming argument
  155.     #
  156.     ESSID=$*
  157.  
  158.     #
  159.     # Kill previous instances of wpa_supplicant to stop other instances wpa_supplicant fighting several different AP's
  160.     # Kill based on ref: http://thegeekstuff.com/2011/10/grep-or-and-not-operators/ and  http://stackoverflow.com/questions/3510673/find-and-kill-a-process-in-one-line-using-bash-and-regex
  161.     # Release dhcp ip's and bring down the interface
  162.     #
  163.     kill $(ps aux | grep -E '[w]pa_supplicant.*\'$INT'' |  awk '{print $2}') | xargs
  164.     dhclient $INT -r
  165.     ifconfig $INT down
  166.  
  167.     #
  168.     # Assign new credentials to interface
  169.     #
  170.     iwconfig $INT mode managed essid "$ESSID"
  171.     printf "Configured interface $INT; ESSID is $ESSID\n"
  172.     ifconfig $INT up
  173.     printf "Interface $INT is up\n"
  174.     wpa_supplicant -B -Dwext -i$INT -c/etc/wpa_supplicant.conf 2>/dev/null | xargs
  175.     printf "wpa_supplicant running, sleeping for 15...\n"
  176.  
  177.     #
  178.     # Wait to connect before asking for a ip address
  179.     #
  180.     sleep 15
  181.     printf "Running dhclient\n"
  182.     dhclient $INT
  183.  
  184.     #
  185.     # Show current ip for interface
  186.     #
  187.     ifconfig $INT | grep inet
  188. exit
  189. }
  190.  
  191. #
  192. # Start here
  193. #
  194. read_saved
  195.  
  196. exit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement