Advertisement
opexxx

starter-wifi.sh

Jul 9th, 2014
424
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.48 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Uncomment below to enable debug
  4. #set -x
  5. set -e
  6. #set -u
  7.  
  8. declare -r LOG_DEFAULT_COLOR="\033[0m"
  9. declare -r LOG_INFO_COLOR="\033[0m"
  10. declare -r LOG_ERROR_COLOR="\033[1;31m"
  11.  
  12. function log {
  13.   echo "[$(date +"%Y-%m-%d %H:%M:%S %Z")] $@";
  14. }
  15. function info {
  16.   echo -e "${LOG_INFO_COLOR}$(log $@)${LOG_DEFAULT_COLOR}";
  17. }
  18. function error {
  19.   echo -e "${LOG_ERROR_COLOR}$(log $@)${LOG_DEFAULT_COLOR}";
  20. }
  21.  
  22. # Generic waitFor function
  23. # Accepts two parameters:
  24. # - ${1} is condition to wait for
  25. # - ${2} is wait time, defaults to 30 retries (=~ seconds)
  26. function waitFor {
  27.   retries=${2:-30}
  28.   while [ $retries -gt 0 ]; do
  29.     ${1} && info "${1} success" && return 0
  30.     retries=$((retries - 1))
  31.     info "Waiting for ${1}"
  32.     sleep 1
  33.   done
  34.   error "${1} failed"
  35.   return 1
  36. }
  37.  
  38. # Check if wifi has connection
  39. function hasWifi {
  40.   #ping -q -t 1 -c 1 $(netstat -nr | grep default | awk '{print $2}') >/dev/null 2>&1
  41.   nc -G 2 -w 2 -z 1.1.1.1 80 >/dev/null 2>&1
  42. }
  43.  
  44. # Accepts Stater Wifi usage terms
  45. function acceptTerms {
  46.   curl -X POST --data "buttonClicked=4&redirect_url=&err_flag=0" -k https://1.1.1.1/login.html >/dev/null 2>&1
  47. }
  48.  
  49. # Restart wifi and wait till it's available
  50. function bounceWifi {
  51.   info "Bouncing wifi"
  52.   networksetup -setairportpower en0 off
  53.   networksetup -setairportpower en0 on
  54.  
  55.   waitFor hasWifi
  56.   info "Bouncing connected"
  57. }
  58.  
  59. # Check if wifi is connected, bounce otherwise
  60. hasWifi || bounceWifi
  61. # Accept terms
  62. waitFor acceptTerms
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement