daily pastebin goal
31%
SHARE
TWEET

connect.sh

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