SHOW:
|
|
- or go back to the newest paste.
| 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. |
| 128 | + | # Check for an internet connection using curl to download from a |
| 129 | - | # Ref: http://superuser.com/questions/545865/how-to-check-if-internet-connection-is-available-via-terminal/625888#625888 |
| 129 | + | # FQDN. 5 Second timeout. |
| 130 | # Ref: superuser.com/questions/545865/how-to-check-if-internet-\ | |
| 131 | # connection-is-available-via-terminal/625888 | |
| 132 | # | |
| 133 | printf "Checking for internet access...\n" | |
| 134 | curl --interface $INT -o /dev/null -m 5 -s http://www.google.com | |
| 135 | if [[ $? == 0 ]]; then | |
| 136 | INET_AVAIL=1 | |
| 137 | else | |
| 138 | # | |
| 139 | # Check again for internet access | |
| 140 | # | |
| 141 | printf "Rechecking for internet access...\n" | |
| 142 | curl --interface $INT -o /dev/null -m 5 -s http://www.google.com | |
| 143 | if [[ $? == 0 ]]; then | |
| 144 | INET_AVAIL=1 | |
| 145 | else | |
| 146 | INET_AVAIL=0 | |
| 147 | fi | |
| 148 | fi | |
| 149 | } | |
| 150 | ||
| 151 | ||
| 152 | - | # Accepts a filename argument to read a saved file. Also can search or use previous saved |
| 152 | + | |
| 153 | - | # wpa configuration files. Will automatically bypass new config prompting if RANDOM_CONF_MODE |
| 153 | + | |
| 154 | # Accepts a filename argument to read a saved file. Also can search | |
| 155 | - | # Filename checking involves testing file is greater than zero length and exists |
| 155 | + | # or use previous saved |
| 156 | # wpa configuration files. Will automatically bypass new config | |
| 157 | # prompting if RANDOM_CONF_MODE | |
| 158 | # is being used. | |
| 159 | # Filename checking involves testing file is greater than zero | |
| 160 | # length and exists | |
| 161 | # before writing the configuration to wpa_supplicant.conf | |
| 162 | # | |
| 163 | ||
| 164 | local CONF_FILE=$1 | |
| 165 | ||
| 166 | if [ $RANDOM_CONF_MODE -eq 0 ]; then | |
| 167 | if [ -n "$CONF_FILE" ]; then | |
| 168 | if [ -s "$CONF_FILE" ]; then | |
| 169 | printf "File $CONF_FILE exists, proceeding to connect\n" | |
| 170 | WRITE_CONF "$CONF_FILE" | |
| 171 | exit 0 | |
| 172 | else | |
| 173 | printf "File $CONF_FILE is invalid or does not exist\n" | |
| 174 | CONF_CREATE | |
| 175 | exit 0 | |
| 176 | fi | |
| 177 | fi | |
| 178 | else | |
| 179 | if [ -n "$CONF_FILE" ]; then | |
| 180 | if [ -s "$CONF_FILE" ]; then | |
| 181 | WRITE_CONF "$CONF_FILE" | |
| 182 | exit 1 | |
| 183 | else | |
| 184 | printf "File $CONF_FILE is invalid or does not exist\n" | |
| 185 | RANDOM_CONF | |
| 186 | exit 1 | |
| 187 | fi | |
| 188 | else | |
| 189 | RANDOM_CONF | |
| 190 | exit 1 | |
| 191 | fi | |
| 192 | fi | |
| 193 | ||
| 194 | # | |
| 195 | # Save and change IFS so spaces in file names are not interpreted as | |
| 196 | # separate lines in the array | |
| 197 | # | |
| 198 | - | # ref:http://www.cyberciti.biz/tips/handling-filenames-with-spaces-in-bash.html |
| 198 | + | |
| 199 | IFS=$'\n' | |
| 200 | ||
| 201 | # | |
| 202 | - | # ref:http://serverfault.com/questions/354403/remove-path-from-find-command-output |
| 202 | + | |
| 203 | # ref:http://www.cyberciti.biz/tips/handling-filenames-with-spaces-\ | |
| 204 | # in-bash.html | |
| 205 | # | |
| 206 | # " -printf '%f\n' " removes path info from outputs | |
| 207 | # | |
| 208 | # ref:http://serverfault.com/questions/354403/remove-path-from-find\ | |
| 209 | # -command-output | |
| 210 | # | |
| 211 | SAVED_LIST=($(find . -type f -name "*.wpa" -printf '%f\n')) | |
| 212 | ||
| 213 | # | |
| 214 | # restore ifs | |
| 215 | # | |
| 216 | IFS=$OLDIFS | |
| 217 | ||
| 218 | # | |
| 219 | # Tests for number of saved wifi connections, if none exit | |
| 220 | # | |
| 221 | if [ -z "${SAVED_LIST[0]}" ]; then
| |
| 222 | printf "There are no previous saved wifi connections\n" | |
| 223 | # | |
| 224 | # Create new connection | |
| 225 | # | |
| 226 | CONF_CREATE | |
| 227 | exit 0 | |
| 228 | fi | |
| 229 | - | #Select one of the previous saved configurations to connect with or quit |
| 229 | + | |
| 230 | # | |
| 231 | #PS3 Sets the prompt for the select statement below | |
| 232 | # | |
| 233 | PS3="Choose a previously saved wifi connection or 's' to skip: " | |
| 234 | ||
| 235 | # | |
| 236 | # Select one of the previous saved configurations to connect with or | |
| 237 | # quit | |
| 238 | # | |
| 239 | select CONF_FILE in "${SAVED_LIST[@]}"; do
| |
| 240 | # | |
| 241 | # Quit if selected number does not exist | |
| 242 | # | |
| 243 | if [ -z "$CONF_FILE" ] ; then | |
| 244 | printf "Skipping\n\n" | |
| 245 | PS3="" | |
| 246 | CONF_CREATE | |
| 247 | exit 0 | |
| 248 | fi | |
| 249 | # | |
| 250 | # Quick check if file is greater than zero length and exists | |
| 251 | # | |
| 252 | if [ -s "$CONF_FILE" ]; then | |
| 253 | printf "File $CONF_FILE exists, proceeding to connect\n" | |
| 254 | PS3="" | |
| 255 | WRITE_CONF "$CONF_FILE" | |
| 256 | exit 0 | |
| 257 | else | |
| 258 | printf "File $CONF_FILE is invalid or does not exist, proceeding to create a new configuration\n" | |
| 259 | PS3="" | |
| 260 | CONF_CREATE | |
| 261 | - | # This function randomly select a previously saved config files, check for internet access and keep trying another |
| 261 | + | |
| 262 | - | # connections if internet access is not made. If and internet connection is not found, it tries another |
| 262 | + | |
| 263 | - | # connection. Could be used as a faiover by specifying -f as your initial preferred network. |
| 263 | + | |
| 264 | } | |
| 265 | ||
| 266 | ||
| 267 | function RANDOM_CONF (){
| |
| 268 | # | |
| 269 | - | # Test to check if RANDOM_FIRST_RUN is 1. If so then proceed to connect using the config file specified first. |
| 269 | + | # This function randomly select a previously saved config files, |
| 270 | # check for internet access and keep trying another | |
| 271 | # connections if internet access is not made. If and internet | |
| 272 | # connection is not found, it tries another | |
| 273 | # connection. Could be used as a faiover by specifying -f as your | |
| 274 | # initial preferred network. | |
| 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 |
| 277 | + | |
| 278 | - | # a internet connection. If there is no connection then it go to the random config selection. |
| 278 | + | |
| 279 | # | |
| 280 | # Test to check if RANDOM_FIRST_RUN is 1. If so then proceed to | |
| 281 | # connect using the config file specified first. | |
| 282 | # | |
| 283 | if [ $RANDOM_FIRST_RUN -eq 1 ]; then | |
| 284 | RANDOM_FIRST_RUN=0 | |
| 285 | READ_SAVED "$CONF_FILE" | |
| 286 | fi | |
| 287 | ||
| 288 | # | |
| 289 | # Test if INET_AVAIL is 1, initial value is 0 and if connection is | |
| 290 | # good then it is 1. Used to loop and recheck for | |
| 291 | # a internet connection. If there is no connection then it go to | |
| 292 | # the random config selection. | |
| 293 | # | |
| 294 | ||
| 295 | if [ $INET_AVAIL -eq 1 ]; then | |
| 296 | # | |
| 297 | # Sleep for a random period of 600 to 900 seconds | |
| 298 | # | |
| 299 | sleep $(((RANDOM % 300)+600)) | |
| 300 | PINGCHECK | |
| 301 | - | # This is a user entry prompt function, used to create new config files. |
| 301 | + | |
| 302 | else | |
| 303 | OLDIFS=$IFS | |
| 304 | IFS=$'\n' | |
| 305 | CONF_FILE=($(find . -type f -name "*.wpa" -printf '%f\n'|sort -R |tail -1)) | |
| 306 | IFS=$OLDIFS | |
| 307 | READ_SAVED "$CONF_FILE" | |
| 308 | fi | |
| 309 | exit 1 | |
| 310 | } | |
| 311 | ||
| 312 | ||
| 313 | function CONF_CREATE (){
| |
| 314 | # | |
| 315 | # This is a user entry prompt function, used to create new config | |
| 316 | # files. | |
| 317 | # | |
| 318 | ||
| 319 | # | |
| 320 | # Scans for wifi connections & isolates wifi AP name | |
| 321 | # | |
| 322 | eval LIST=( $(iwlist $INT scan 2>/dev/null | awk -F":" '/ESSID/{print $2}') )
| |
| 323 | ||
| 324 | # | |
| 325 | #PS3 Sets the prompt for the select statement below | |
| 326 | # | |
| 327 | PS3="Choose a new ESSID to create a connection or 'q' to quit: " | |
| 328 | ||
| 329 | # | |
| 330 | # Tests for number of wifi connections, exits if none | |
| 331 | # | |
| 332 | if [ -z "${LIST[0]}" ]; then
| |
| 333 | printf "No available wifi connection using $INT\n" | |
| 334 | exit 1 | |
| 335 | fi | |
| 336 | ||
| 337 | # | |
| 338 | # Select from a LIST of scanned connections | |
| 339 | # | |
| 340 | select CONF_FILE in "${LIST[@]}"; do
| |
| 341 | ||
| 342 | - | # Append the CONF_FILE variable (ESSID) to .wpa to make a filename |
| 342 | + | |
| 343 | - | # for saved configs |
| 343 | + | |
| 344 | # | |
| 345 | if [ -z "$CONF_FILE" ] ; then | |
| 346 | printf "Exiting\n" | |
| 347 | exit 0 | |
| 348 | fi | |
| 349 | ||
| 350 | # | |
| 351 | # Get user input for passphrase no need to escape spaces | |
| 352 | # | |
| 353 | printf "Enter the passphrase for $CONF_FILE?\n" | |
| 354 | read "PASSPHRASE" | |
| 355 | ||
| 356 | # | |
| 357 | # Append the CONF_FILE variable (ESSID) to .wpa to make a | |
| 358 | # filename for saved configs | |
| 359 | # | |
| 360 | FILENAME=$CONF_FILE".wpa" | |
| 361 | ||
| 362 | # | |
| 363 | # Run wpa_passphrase to generate a file for wpa_supplicant to | |
| 364 | # use, store it locally and in etc/wpa_supplicant.conf | |
| 365 | # | |
| 366 | printf "Running wpa_passphrase\n" | |
| 367 | wpa_passphrase "$CONF_FILE" "$PASSPHRASE" > "$FILENAME" | xargs | |
| 368 | # | |
| 369 | # Jump to WRITE_CONF function, append configuration filename | |
| 370 | # to CONF_FILE variable | |
| 371 | # | |
| 372 | ||
| 373 | PS3="" | |
| 374 | WRITE_CONF "$FILENAME" | |
| 375 | exit 1 | |
| 376 | done | |
| 377 | } | |
| 378 | ||
| 379 | ||
| 380 | function WRITE_CONF (){
| |
| 381 | ||
| 382 | local CONF_FILE=$1 | |
| 383 | # | |
| 384 | # Copy local wpa_supplicant file to etc/wpa_supplicant.conf | |
| 385 | # | |
| 386 | printf "Writing new configuration file using $CONF_FILE\n" | |
| 387 | cat "$CONF_FILE">/etc/wpa_supplicant.conf | xargs | |
| 388 | # | |
| 389 | # Jump to connect function, pass on the ESSID variable for connection | |
| 390 | # | |
| 391 | CONNECT "$CONF_FILE" | |
| 392 | exit 1 | |
| 393 | } | |
| 394 | ||
| 395 | ||
| 396 | function CONNECT (){
| |
| 397 | # | |
| 398 | # Capture first incoming argument as SSID | |
| 399 | # | |
| 400 | local ESSID=$1 | |
| 401 | ||
| 402 | # | |
| 403 | # Kill previous instances of wpa_supplicant to stop other instances | |
| 404 | # wpa_supplicant fighting several different AP's | |
| 405 | # Kill based on | |
| 406 | # ref: http://thegeekstuff.com/2011/10/grep-or-and-not-operators | |
| 407 | # and | |
| 408 | # http://stackoverflow.com/questions/3510673/find-and-kill-a-\ | |
| 409 | # process-in-one-line-using-bash-and-regex | |
| 410 | # | |
| 411 | # Release dhcp ip's and bring down the interface | |
| 412 | # | |
| 413 | kill $(ps aux | grep -E '[w]pa_supplicant.*\'$INT'' | awk '{print $2}') 2>/dev/null | xargs
| |
| 414 | dhclient $INT -r | |
| 415 | ifconfig $INT down | |
| 416 | ||
| 417 | # | |
| 418 | # Assign new credentials to config file | |
| 419 | # | |
| 420 | iwconfig $INT mode managed essid "$ESSID" | |
| 421 | printf "Configured interface $INT; Config file is $ESSID\n" | |
| 422 | ifconfig $INT up | |
| 423 | printf "Interface $INT is up\n" | |
| 424 | wpa_supplicant -B -Dwext -i$INT -c/etc/wpa_supplicant.conf 2>/dev/null | xargs | |
| 425 | printf "wpa_supplicant running, sleeping for 15...\n" | |
| 426 | ||
| 427 | # | |
| 428 | # Wait to connect before asking for a ip address | |
| 429 | # | |
| 430 | sleep 15 | |
| 431 | printf "Running dhclient\n" | |
| 432 | dhclient $INT | |
| 433 | ||
| 434 | # | |
| 435 | # Print current ip for interface to screen | |
| 436 | # | |
| 437 | ifconfig $INT | grep inet | |
| 438 | ||
| 439 | # | |
| 440 | # Check if internet is available and print status to screen | |
| 441 | # | |
| 442 | sleep 5 | |
| 443 | PINGCHECK | |
| 444 | if [ $INET_AVAIL -eq 1 ]; then | |
| 445 | printf "Internet is available on this connection\n" | |
| 446 | else | |
| 447 | printf "Internet is unavailable on this connection\n" | |
| 448 | fi | |
| 449 | ||
| 450 | if [ $RANDOM_CONF_MODE -eq 1 ]; then | |
| 451 | - | # Check if RANDOM_CONF_MODE is selected and/or config file specified, routes to correct function set |
| 451 | + | |
| 452 | fi | |
| 453 | ||
| 454 | exit 0 | |
| 455 | } | |
| 456 | ||
| 457 | ######################################################################## | |
| 458 | ######################## START HERE #################################### | |
| 459 | ||
| 460 | # | |
| 461 | # Call check args function to check the command line argument supplied are valid. | |
| 462 | # | |
| 463 | CHECK_ARGS "$@" | |
| 464 | ||
| 465 | # | |
| 466 | # Check if RANDOM_CONF_MODE is selected and/or config file specified, | |
| 467 | # routes to correct function set | |
| 468 | # with file name arguments or no arguments depending on what is specified. | |
| 469 | # | |
| 470 | if [ $RANDOM_CONF_MODE -eq 1 ]; then | |
| 471 | if [ "$CONF_FILE" ]; then | |
| 472 | RANDOM_FIRST_RUN=1 | |
| 473 | RANDOM_CONF "$CONF_FILE" | |
| 474 | else | |
| 475 | RANDOM_CONF | |
| 476 | fi | |
| 477 | else | |
| 478 | if [ "$CONF_FILE" ]; then | |
| 479 | READ_SAVED "$CONF_FILE" | |
| 480 | else | |
| 481 | READ_SAVED | |
| 482 | fi | |
| 483 | fi | |
| 484 | ||
| 485 | exit 0 |