Advertisement
Guest User

connect.sh

a guest
Oct 23rd, 2013
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2.  
  3. # 23/10/2013
  4. # This script attempts to semi-automate / automate the wifi connection
  5. # process from 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 version 1  http://pastebin.com/Pa90HBiU  01/06/2013
  24. #   very simple first version
  25. # old version 2  http://pastebin.com/FzJnv5Nk  02/06/2013
  26. #   minor additions
  27. # old version 3  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. # old version 4
  32. #   Added exit return values
  33. # Current version 5
  34. #   Removal of cleanslate function.  Capitalising of function names.
  35. #   Renaming of some function names.
  36. #   Addition of random_config to randomly select a previously saved
  37. #   config files, check for internet access and keep trying another
  38. #   connections if internet access is not made.  Can be used in
  39. #   conjunction with -f to initially select a conf_file of your choice.
  40. #   Improved program feedback and internet access checking.
  41. #
  42. # This script uses the following commands: find, printf, iwlist, awk,
  43. # cat, kill, ps, grep, xargs, iwconfig, dhclient, wpa_supplicant, curl.
  44. #
  45. # Copy, Distribute and Modify Freely.
  46. #
  47.  
  48.  
  49. ########################################################################
  50. ######################## FUNCTIONS #####################################
  51.  
  52. function HELP (){
  53.     HELP_MESSAGE="$1"
  54.     if [ "$HELP_MESSAGE" ]; then
  55.         printf "$HELP_MESSAGE"
  56.     fi
  57.     printf "Usage: $0 -i [INTERFACE] OR AND -f [CONF_FILE.wpa] -r (RANDOM_CONF_MODE)\n"
  58. }
  59.  
  60.  
  61. function CHECK_ARGS () {
  62.     #
  63.     # Define Globals
  64.     #
  65.     INET_AVAIL=0
  66.     RANDOM_CONF_MODE=0
  67.     RANDOM_FIRST_RUN=0
  68.     CONF_FILE=""
  69.     INT=""
  70.  
  71.     if [ -z "$1" ]; then
  72.     HELP
  73.     exit 1
  74.     fi
  75.  
  76.     while getopts  "i:f:r" opt
  77.         do
  78.             case $opt in
  79.                 i ) INT=${OPTARG};;
  80.                 f ) CONF_FILE=${OPTARG};;
  81.                 r ) RANDOM_CONF_MODE=1;;
  82.                 \?) HELP
  83.                     exit 1;;
  84.                 * ) HELP
  85.                     exit 1;;
  86.             esac
  87.         done
  88.  
  89.     #
  90.     # check if root
  91.     #
  92.     if [ "$(id -u)" != "0" ]; then
  93.         HELP "This script must be run as root\n"
  94.         exit 1
  95.     fi
  96.  
  97.     #
  98.     # check if interface is entered as command line argument
  99.     #
  100.     if [ -z "$INT" ]; then
  101.         HELP
  102.         exit 1
  103.     fi
  104.  
  105.     #
  106.     # Check if interface entered exists
  107.     #
  108.     if [ ! -e /sys/class/net/$INT ]; then
  109.         HELP "Network Interface $INT does not exist\n"
  110.         exit 1
  111.     fi
  112.  
  113.     #
  114.     # If random mode selected, check if wpa files are available
  115.     #
  116.     if [ $RANDOM_CONF_MODE -eq 1 ]; then
  117.         local WPA_FILES=$(find . -type f -name "*.wpa" | wc -l)
  118.         if [ "$WPA_FILES" -eq 0  ]; then
  119.             HELP "No *.wpa files exist for RANDOM_CONF_MODE\n"
  120.             exit 1
  121.         fi
  122.     fi
  123. }
  124.  
  125.  
  126. function PINGCHECK (){
  127.     #
  128.     # Check for an internet connection using curl to download from a FQDN. 5 Second timeout.
  129.     # Ref: http://superuser.com/questions/545865/how-to-check-if-internet-connection-is-available-via-terminal/625888#625888
  130.     #
  131.     printf "Checking for internet access...\n"
  132.     curl --interface $INT -o /dev/null -m 5 -s http://www.google.com
  133.     if [[ $? == 0 ]]; then
  134.         INET_AVAIL=1
  135.     else
  136.         #
  137.         # Check again for internet access
  138.         #
  139.         printf "Rechecking for internet access...\n"
  140.         curl --interface $INT -o /dev/null -m 5 -s http://www.google.com
  141.         if [[ $? == 0 ]]; then
  142.             INET_AVAIL=1
  143.         else
  144.             INET_AVAIL=0
  145.         fi
  146.     fi
  147. }
  148.  
  149.  
  150. function READ_SAVED (){
  151.     #
  152.     # Accepts a filename argument to read a saved file.  Also can search or use previous saved
  153.     # wpa configuration files.  Will automatically bypass new config prompting if RANDOM_CONF_MODE
  154.     # is being used.
  155.     # Filename checking involves testing file is greater than zero length and exists
  156.     # before writing the configuration to wpa_supplicant.conf
  157.     #
  158.  
  159.     local CONF_FILE=$1
  160.  
  161.     if [ $RANDOM_CONF_MODE -eq 0 ]; then
  162.         if [ -n "$CONF_FILE" ]; then
  163.             if [ -s "$CONF_FILE" ]; then
  164.                 printf "File $CONF_FILE exists, proceeding to connect\n"
  165.                 WRITE_CONF "$CONF_FILE"
  166.                 exit 0
  167.             else
  168.                 printf "File $CONF_FILE is invalid or does not exist\n"
  169.                 CONF_CREATE
  170.                 exit 0
  171.             fi
  172.         fi
  173.     else
  174.         if [ -n "$CONF_FILE" ]; then
  175.             if [ -s "$CONF_FILE" ]; then
  176.                 WRITE_CONF "$CONF_FILE"
  177.                 exit 1
  178.             else
  179.                 printf "File $CONF_FILE is invalid or does not exist\n"
  180.                 RANDOM_CONF
  181.                 exit 1
  182.             fi
  183.         else
  184.             RANDOM_CONF
  185.             exit 1
  186.         fi
  187.     fi
  188.  
  189.     #
  190.     # Save and change IFS so spaces in file names are not interpreted as
  191.     # separate lines in the array
  192.     #
  193.     OLDIFS=$IFS
  194.     IFS=$'\n'
  195.  
  196.     #
  197.     # Read all file names into an array
  198.     # ref:http://www.cyberciti.biz/tips/handling-filenames-with-spaces-in-bash.html
  199.     #
  200.     # " -printf '%f\n' " removes path info from outputs
  201.     #
  202.     # ref:http://serverfault.com/questions/354403/remove-path-from-find-command-output
  203.     #
  204.     SAVED_LIST=($(find . -type f -name "*.wpa" -printf '%f\n'))
  205.  
  206.     #
  207.     # restore ifs
  208.     #
  209.     IFS=$OLDIFS
  210.  
  211.     #
  212.     # Tests for number of saved wifi connections, if none exit
  213.     #
  214.     if [ -z "${SAVED_LIST[0]}" ]; then
  215.         printf "There are no previous saved wifi connections\n"
  216.         #
  217.         # Create new connection
  218.         #
  219.         CONF_CREATE
  220.         exit 0
  221.     fi
  222.  
  223.     #
  224.     #PS3 Sets the prompt for the select statement below
  225.     #
  226.     PS3="Choose a previously saved wifi connection or 's' to skip: "
  227.  
  228.     #
  229.     #Select one of the previous saved configurations to connect with or quit
  230.     #
  231.     select CONF_FILE in "${SAVED_LIST[@]}"; do
  232.         #
  233.         # Quit if selected number does not exist
  234.         #
  235.         if [ -z "$CONF_FILE" ] ; then
  236.                 printf "Skipping\n\n"
  237.                 PS3=""
  238.                 CONF_CREATE
  239.                 exit 0
  240.         fi
  241.         #
  242.         # Quick check if file is greater than zero length and exists
  243.         #
  244.         if [ -s "$CONF_FILE" ]; then
  245.             printf "File $CONF_FILE exists, proceeding to connect\n"
  246.             PS3=""
  247.             WRITE_CONF "$CONF_FILE"
  248.             exit 0
  249.             else
  250.             printf "File $CONF_FILE is invalid or does not exist, proceeding to create a new configuration\n"
  251.             PS3=""
  252.             CONF_CREATE
  253.             exit 0
  254.         fi
  255.     done
  256. }
  257.  
  258.  
  259. function RANDOM_CONF (){
  260.     #
  261.     # This function randomly select a previously saved config files, check for internet access and keep trying another
  262.     # connections if internet access is not made.  If and internet connection is not found, it tries another
  263.     # connection.  Could be used as a faiover by specifying -f as your initial preferred network.
  264.     #
  265.  
  266.     local CONF_FILE=$1
  267.  
  268.     #
  269.     # Test to check if RANDOM_FIRST_RUN is 1.  If so then proceed to connect using the config file specified first.
  270.     #
  271.     if [ $RANDOM_FIRST_RUN -eq 1 ]; then
  272.         RANDOM_FIRST_RUN=0
  273.         READ_SAVED "$CONF_FILE"
  274.     fi
  275.  
  276.     #
  277.     # Test if INET_AVAIL is 1, initial value is 0 and if connection is good then it is 1.  Used to loop and recheck for
  278.     # a internet connection.  If there is no connection then it go to the random config selection.
  279.     #
  280.  
  281.     if [ $INET_AVAIL -eq 1 ]; then
  282.         #
  283.         # Sleep for a random period of 600 to 900 seconds
  284.         #
  285.         sleep $(((RANDOM % 300)+600))
  286.         PINGCHECK
  287.         RANDOM_CONF
  288.     else
  289.         OLDIFS=$IFS
  290.         IFS=$'\n'
  291.         CONF_FILE=($(find . -type f -name "*.wpa" -printf '%f\n'|sort -R |tail -1))
  292.         IFS=$OLDIFS
  293.         READ_SAVED "$CONF_FILE"
  294.     fi
  295.     exit 1
  296. }
  297.  
  298.  
  299. function CONF_CREATE (){
  300.     #
  301.     # This is a user entry prompt function, used to create new config files.
  302.     #
  303.  
  304.     #
  305.     # Scans for wifi connections & isolates wifi AP name
  306.     #
  307.     eval LIST=( $(iwlist $INT scan 2>/dev/null | awk -F":" '/ESSID/{print $2}') )
  308.  
  309.     #
  310.     #PS3 Sets the prompt for the select statement below
  311.     #
  312.     PS3="Choose a new ESSID to create a connection or 'q' to quit: "
  313.  
  314.     #
  315.     # Tests for number of wifi connections, exits if none
  316.     #
  317.         if [ -z "${LIST[0]}" ]; then
  318.             printf "No available wifi connection using $INT\n"
  319.             exit 1
  320.         fi
  321.  
  322.     #
  323.     # Select from a LIST of scanned connections
  324.     #
  325.     select CONF_FILE in "${LIST[@]}"; do
  326.  
  327.         #
  328.         # Quit if selected number does not exist or alpha in entered
  329.         #
  330.         if [ -z "$CONF_FILE" ] ; then
  331.                 printf "Exiting\n"
  332.                 exit 0
  333.         fi
  334.  
  335.         #
  336.         # Get user input for passphrase no need to escape spaces
  337.         #
  338.         printf "Enter the passphrase for $CONF_FILE?\n"
  339.         read "PASSPHRASE"
  340.  
  341.         #
  342.         # Append the CONF_FILE variable (ESSID) to .wpa to make a filename
  343.         # for saved configs
  344.         #
  345.         FILENAME=$CONF_FILE".wpa"
  346.  
  347.         #
  348.         # Run wpa_passphrase to generate a file for wpa_supplicant to
  349.         # use, store it locally and in etc/wpa_supplicant.conf
  350.         #
  351.         printf "Running wpa_passphrase\n"
  352.         wpa_passphrase "$CONF_FILE" "$PASSPHRASE" > "$FILENAME" | xargs
  353.         #
  354.         # Jump to WRITE_CONF function, append configuration filename
  355.         # to CONF_FILE variable
  356.         #
  357.  
  358.         PS3=""
  359.         WRITE_CONF "$FILENAME"
  360.         exit 1
  361.     done
  362. }
  363.  
  364.  
  365. function WRITE_CONF (){
  366.  
  367.     local CONF_FILE=$1
  368.     #
  369.     # Copy local wpa_supplicant file to etc/wpa_supplicant.conf
  370.     #
  371.     printf "Writing new configuration file using $CONF_FILE\n"
  372.     cat "$CONF_FILE">/etc/wpa_supplicant.conf | xargs
  373.     #
  374.     # Jump to connect function, pass on the ESSID variable for connection
  375.     #
  376.     CONNECT "$CONF_FILE"
  377.     exit 1
  378. }
  379.  
  380.  
  381. function CONNECT (){
  382.     #
  383.     # Capture first incoming argument as SSID
  384.     #
  385.     local ESSID=$1
  386.  
  387.     #
  388.     # Kill previous instances of wpa_supplicant to stop other instances
  389.     # wpa_supplicant fighting several different AP's
  390.     # Kill based on
  391.     # ref: http://thegeekstuff.com/2011/10/grep-or-and-not-operators
  392.     # and
  393.     # http://stackoverflow.com/questions/3510673/find-and-kill-a-\
  394.     # process-in-one-line-using-bash-and-regex
  395.     #
  396.     # Release dhcp ip's and bring down the interface
  397.     #
  398.     kill $(ps aux | grep -E '[w]pa_supplicant.*\'$INT'' |  awk '{print $2}') 2>/dev/null | xargs
  399.     dhclient $INT -r
  400.     ifconfig $INT down
  401.  
  402.     #
  403.     # Assign new credentials to config file
  404.     #
  405.     iwconfig $INT mode managed essid "$ESSID"
  406.     printf "Configured interface $INT; Config file is $ESSID\n"
  407.     ifconfig $INT up
  408.     printf "Interface $INT is up\n"
  409.     wpa_supplicant -B -Dwext -i$INT -c/etc/wpa_supplicant.conf 2>/dev/null | xargs
  410.     printf "wpa_supplicant running, sleeping for 15...\n"
  411.  
  412.     #
  413.     # Wait to connect before asking for a ip address
  414.     #
  415.     sleep 15
  416.     printf "Running dhclient\n"
  417.     dhclient $INT
  418.  
  419.     #
  420.     # Print current ip for interface to screen
  421.     #
  422.     ifconfig $INT | grep inet
  423.  
  424.     #
  425.     # Check if internet is available and print status to screen
  426.     #
  427.     sleep 5
  428.     PINGCHECK
  429.     if [ $INET_AVAIL -eq 1 ]; then
  430.         printf "Internet is available on this connection\n"
  431.     else
  432.         printf "Internet is unavailable on this connection\n"
  433.     fi
  434.  
  435.     if [ $RANDOM_CONF_MODE -eq 1 ]; then
  436.     RANDOM_CONF
  437.     fi
  438.  
  439.     exit 0
  440. }
  441.  
  442. ########################################################################
  443. ######################## START HERE ####################################
  444.  
  445. #
  446. # Call check args function to check the command line argument supplied are valid.
  447. #
  448. CHECK_ARGS "$@"
  449.  
  450. #
  451. # Check if RANDOM_CONF_MODE is selected and/or config file specified, routes to correct function set
  452. # with file name arguments or no arguments depending on what is specified.
  453. #
  454. if [ $RANDOM_CONF_MODE -eq 1 ]; then
  455.     if [ "$CONF_FILE" ]; then
  456.         RANDOM_FIRST_RUN=1
  457.         RANDOM_CONF "$CONF_FILE"
  458.     else
  459.     RANDOM_CONF
  460.     fi
  461. else
  462.     if [ "$CONF_FILE" ]; then
  463.         READ_SAVED "$CONF_FILE"
  464.     else
  465.         READ_SAVED
  466.     fi
  467. fi
  468.  
  469. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement