Advertisement
Guest User

Untitled

a guest
Apr 6th, 2020
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.25 KB | None | 0 0
  1. #!/bin/bash
  2. #
  3. # Autosets system's timezone depending on the public IP.
  4.  
  5.  
  6. LINK_IP="https://api.ipify.org/"
  7. LINK_TZ="https://worldtimeapi.org/api/ip/"
  8. USER_AGENTS=("Mozilla/5.0" "curl/7.67.0" "Wget/1.20.3")
  9. SIZE=${#USER_AGENTS[@]}
  10. REGEX_IP="[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}"
  11. TZ_DEFAULT="UTC"
  12. SOUND="/usr/share/sounds/Oxygen-Sys-Warning.ogg"
  13. VERBOSE=0
  14. PAUSE=3
  15. TIMEOUT=10
  16. LOGFILE=""
  17.  
  18.  
  19. #######################################
  20. # Prints out the usage message on invalid or -h/--help flags,
  21. # Outputs:
  22. #   Echoes usage message to STDOUT.
  23. #######################################
  24. print_usage() {
  25.   echo "IP-to-Timezone switcher: autosets system's timezone on IP change."
  26.   echo "Usage:"
  27.   echo "  [--fallback=] -- sets timezone to fallback to  (default: \"$TZ_DEFAULT\");"
  28.   echo "  [-p=, --pause=] -- sets pause in seconds between IP checks  (default: $PAUSE);"
  29.   echo "  [-t=, --timeout=] -- sets timeout in seconds for cURL queries  (default: $TIMEOUT);"
  30.   echo "  [--logfile=] -- toggles log dumping in a file;"
  31.   echo "  [-v, --verbose] -- toggles verbosity;"
  32.   echo "  [-h, --help] -- prints out this message."
  33. }
  34.  
  35.  
  36. #######################################
  37. # Prints out logs in "[YYYY-MM-DD HH:MM:SS]  MESSAGE" format on -v/--verbose flag.
  38. # Outputs logs in a file, located in :LOGFILE: (if it's not empty).
  39. # Arguments:
  40. #   MESSAGE to log.
  41. # Globals:
  42. #   VERBOSE
  43. #   LOGFILE
  44. # Outputs:
  45. #   Logs out the MESSAGE to STDOUT.
  46. #######################################
  47. log() {
  48.   log_string="[$(date -u '+%F %T')]  $*"
  49.   [ $VERBOSE -eq 1 ] && printf "%s\n" "$log_string";
  50.   [ -n "$LOGFILE" ] && echo "$log_string" >> "$LOGFILE"
  51. }
  52.  
  53.  
  54. #######################################
  55. # Requests the current IP address from :LINK_IP: using randomized user-agent from :USER_AGENTS: list.
  56. # Globals:
  57. #   LINK_IP
  58. #   USER_AGENTS
  59. #   SIZE
  60. # Returns:
  61. #   Response of a cURL request to :LINK_IP:.
  62. #######################################
  63. get_ip() {
  64.   index=$((RANDOM % SIZE))
  65.   ua=${USER_AGENTS[$index]}
  66.   myip=$(curl $LINK_IP --user-agent "$ua" --max-time $TIMEOUT -s)
  67.  
  68.   echo "$myip"
  69. }
  70.  
  71.  
  72. #######################################
  73. # Requests a JSON response from :LINK_TZ: using randomized user-agent from :USER_AGENTS: list,
  74. # gets (and cleans) the value of the "timezone" key,
  75. # sets system's timezone to that value and
  76. # sends a system notification with :SOUND: alarm.
  77. # Globals:
  78. #   LINK_TZ
  79. #   USER_AGENTS
  80. #   SIZE
  81. #   SOUND
  82. # Outputs:
  83. #   Prints out status messages to STDOUT.
  84. #######################################
  85. set_timezone() {
  86.   index=$((RANDOM % SIZE))
  87.   ua=${USER_AGENTS[$index]}
  88.  
  89.   log "Requesting timezone data with user-agent \"$ua\"."
  90.   TZ=$(curl $LINK_TZ --user-agent "$ua" --max-time $TIMEOUT -s | jq '.timezone')
  91.  
  92.   TZ=$(echo "${TZ:=$TZ_DEFAULT}" | cut -d'"' -f 2) &&
  93.   printf "\nGot timezone \"%s\". Setting...\n" "$TZ" &&
  94.   timedatectl set-timezone "$TZ" &&
  95.   notify-send "Successfully changed your timezone to \"$TZ\"." &&
  96.   paplay $SOUND &&
  97.   printf "Successfully set the timezone to \"%s\".\n\n" "$TZ" &&
  98.   log "Successfully got and set system's timezone to \"$TZ\"."
  99. }
  100.  
  101.  
  102. for i in "$@"; do
  103.   case $i in
  104.     --fallback=*)
  105.       TZ_DEFAULT="${i#*=}" ;;
  106.     -p=*| --pause=*)
  107.       PAUSE="${i#*=}" ;;
  108.     -t=*| --timeout=*)
  109.       TIMEOUT="${i#*=}" ;;
  110.     --logfile=*)
  111.       LOGFILE="${i#*=}" ;;
  112.     -v | --verbose)
  113.       VERBOSE=1 ;;
  114.     -h | --help | *)
  115.       print_usage
  116.       exit 0
  117.       ;;
  118.   esac
  119. done
  120.  
  121. log "Running in verbose mode."
  122. log "Will fallback to \"$TZ_DEFAULT\"."
  123. log "CURL will timeout in $TIMEOUT seconds."
  124. log "Will sleep for $PAUSE seconds."
  125. [ -n "$LOGFILE" ] && log "Will log everything to $LOGFILE."
  126.  
  127. log "Trying to get the initial IP..."
  128. myip_start=$(get_ip)
  129. while [[ ! "$myip_start" =~ $REGEX_IP ]]; do
  130.   log "Got invalid IP format: \"$myip_start\". Will continue."
  131.   myip_start=$(get_ip)
  132. done
  133.  
  134. log "Got the initial IP: \"$myip_start\"."
  135. set_timezone
  136.  
  137. while true; do
  138.   log "Updating IP..."
  139.   myip=$(get_ip)
  140.   if [[ ! "$myip" =~ $REGEX_IP ]]; then
  141.     log "Got invalid IP format: \"$myip\". Will continue."
  142.     continue
  143.   fi
  144.   log "Got IP: \"$myip\"."
  145.   [ "$myip" != "$myip_start" ] && set_timezone && myip_start=$myip && log "Successfully updated IP."
  146.  
  147.   sleep "$PAUSE"
  148. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement