Skip to content

Instantly share code, notes, and snippets.

@cmbuckley
Last active June 24, 2020 11:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cmbuckley/17568fe63e056aadc9f52745bd17ad24 to your computer and use it in GitHub Desktop.
Save cmbuckley/17568fe63e056aadc9f52745bd17ad24 to your computer and use it in GitHub Desktop.
Akamai hosts updater
#!/bin/bash
domain=$1
origin="example-origin.net"
file=/etc/hosts
fqdn=
if [ $# -lt 1 ]; then
echo "Usage: $(basename "$0") DOMAIN [{production|staging|origin|off}]" >&2
exit 1
fi
if [ $# -eq 1 ]; then
grep "$domain " "$file" || echo "$domain: no manual host entry"
exit
fi
sudo -v || exit 1
sudo sed -i '.bak' "/$domain/d" "$file"
# base string for edge hostname
base=$(dig +short "$domain" | head -1 | sed 's/.edgekey.net.$//')
[ -z "$base" ] && base=$domain
case "$2" in
prod|production|live)
fqdn="$base.edgekey.net"
;;
stg|staging)
fqdn="$base.edgekey-staging.net"
;;
origin)
fqdn="$domain.$origin"
;;
esac
if [ -n "$fqdn" ]; then
ip=$(dig +short "$fqdn" | grep -v '\.$' | tail -n 1)
[ -n "$ip" ] && printf "%-15s %-30s # %s\n" "$ip" "$domain" "$fqdn" | sudo tee -a $file >/dev/null || echo "$fqdn: not found" >&2
fi
diff --ignore-all-space -u "$file.bak" "$file" | grep '^[+-]' || echo "No changes were made"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment