SHARE
TWEET

connect.sh

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