Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- #
- # Autosets system's timezone depending on the public IP.
- LINK_IP="https://api.ipify.org/"
- LINK_TZ="https://worldtimeapi.org/api/ip/"
- USER_AGENTS=("Mozilla/5.0" "curl/7.67.0" "Wget/1.20.3")
- SIZE=${#USER_AGENTS[@]}
- REGEX_IP="[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}"
- TZ_DEFAULT="UTC"
- SOUND="/usr/share/sounds/Oxygen-Sys-Warning.ogg"
- VERBOSE=0
- PAUSE=3
- TIMEOUT=10
- LOGFILE=""
- #######################################
- # Prints out the usage message on invalid or -h/--help flags,
- # Outputs:
- # Echoes usage message to STDOUT.
- #######################################
- print_usage() {
- echo "IP-to-Timezone switcher: autosets system's timezone on IP change."
- echo "Usage:"
- echo " [--fallback=] -- sets timezone to fallback to (default: \"$TZ_DEFAULT\");"
- echo " [-p=, --pause=] -- sets pause in seconds between IP checks (default: $PAUSE);"
- echo " [-t=, --timeout=] -- sets timeout in seconds for cURL queries (default: $TIMEOUT);"
- echo " [--logfile=] -- toggles log dumping in a file;"
- echo " [-v, --verbose] -- toggles verbosity;"
- echo " [-h, --help] -- prints out this message."
- }
- #######################################
- # Prints out logs in "[YYYY-MM-DD HH:MM:SS] MESSAGE" format on -v/--verbose flag.
- # Outputs logs in a file, located in :LOGFILE: (if it's not empty).
- # Arguments:
- # MESSAGE to log.
- # Globals:
- # VERBOSE
- # LOGFILE
- # Outputs:
- # Logs out the MESSAGE to STDOUT.
- #######################################
- log() {
- log_string="[$(date -u '+%F %T')] $*"
- [ $VERBOSE -eq 1 ] && printf "%s\n" "$log_string";
- [ -n "$LOGFILE" ] && echo "$log_string" >> "$LOGFILE"
- }
- #######################################
- # Requests the current IP address from :LINK_IP: using randomized user-agent from :USER_AGENTS: list.
- # Globals:
- # LINK_IP
- # USER_AGENTS
- # SIZE
- # Returns:
- # Response of a cURL request to :LINK_IP:.
- #######################################
- get_ip() {
- index=$((RANDOM % SIZE))
- ua=${USER_AGENTS[$index]}
- myip=$(curl $LINK_IP --user-agent "$ua" --max-time $TIMEOUT -s)
- echo "$myip"
- }
- #######################################
- # Requests a JSON response from :LINK_TZ: using randomized user-agent from :USER_AGENTS: list,
- # gets (and cleans) the value of the "timezone" key,
- # sets system's timezone to that value and
- # sends a system notification with :SOUND: alarm.
- # Globals:
- # LINK_TZ
- # USER_AGENTS
- # SIZE
- # SOUND
- # Outputs:
- # Prints out status messages to STDOUT.
- #######################################
- set_timezone() {
- index=$((RANDOM % SIZE))
- ua=${USER_AGENTS[$index]}
- log "Requesting timezone data with user-agent \"$ua\"."
- TZ=$(curl $LINK_TZ --user-agent "$ua" --max-time $TIMEOUT -s | jq '.timezone')
- TZ=$(echo "${TZ:=$TZ_DEFAULT}" | cut -d'"' -f 2) &&
- printf "\nGot timezone \"%s\". Setting...\n" "$TZ" &&
- timedatectl set-timezone "$TZ" &&
- notify-send "Successfully changed your timezone to \"$TZ\"." &&
- paplay $SOUND &&
- printf "Successfully set the timezone to \"%s\".\n\n" "$TZ" &&
- log "Successfully got and set system's timezone to \"$TZ\"."
- }
- for i in "$@"; do
- case $i in
- --fallback=*)
- TZ_DEFAULT="${i#*=}" ;;
- -p=*| --pause=*)
- PAUSE="${i#*=}" ;;
- -t=*| --timeout=*)
- TIMEOUT="${i#*=}" ;;
- --logfile=*)
- LOGFILE="${i#*=}" ;;
- -v | --verbose)
- VERBOSE=1 ;;
- -h | --help | *)
- print_usage
- exit 0
- ;;
- esac
- done
- log "Running in verbose mode."
- log "Will fallback to \"$TZ_DEFAULT\"."
- log "CURL will timeout in $TIMEOUT seconds."
- log "Will sleep for $PAUSE seconds."
- [ -n "$LOGFILE" ] && log "Will log everything to $LOGFILE."
- log "Trying to get the initial IP..."
- myip_start=$(get_ip)
- while [[ ! "$myip_start" =~ $REGEX_IP ]]; do
- log "Got invalid IP format: \"$myip_start\". Will continue."
- myip_start=$(get_ip)
- done
- log "Got the initial IP: \"$myip_start\"."
- set_timezone
- while true; do
- log "Updating IP..."
- myip=$(get_ip)
- if [[ ! "$myip" =~ $REGEX_IP ]]; then
- log "Got invalid IP format: \"$myip\". Will continue."
- continue
- fi
- log "Got IP: \"$myip\"."
- [ "$myip" != "$myip_start" ] && set_timezone && myip_start=$myip && log "Successfully updated IP."
- sleep "$PAUSE"
- done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement