SHARE
TWEET

connect.sh

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