Skip to content

Instantly share code, notes, and snippets.

@Luzifer
Last active July 18, 2020 19:40
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 Luzifer/e5ec42273382128b43826e1de8386e83 to your computer and use it in GitHub Desktop.
Save Luzifer/e5ec42273382128b43826e1de8386e83 to your computer and use it in GitHub Desktop.
Query Hetzner Cloud-Node for your servers
#!/bin/bash
set -euo pipefail
###
# Query Hetzner Cloud-Node for your servers
#
# Wanna know whether a specific outage affects one of your cloud nodes?
# Make a list of nodes before the outage happens and you'll know whether
# your server is hosted on the node with the outage...
#
# Requirements: mtr (tool), API token for your Hetzner Cloud Project in
# ENV variable HETZNER_TOKEN
#
# License: None. It's just some shell commands glued together. Do with
# it what you want, just don't blame me if everything goes south.
###
declare -A hosts
function check_env() {
command -v mtr &>/dev/null || {
echo "Missing MTR utility" >&2
exit 1
}
[[ -n ${HETZNER_TOKEN:-} ]] || {
echo 'Missing Hetzner API Token in $HETZNER_TOKEN' >&2
exit 1
}
}
function fetch_hosts() {
for iphost in $(
curl -sSfL \
-H "Authorization: Bearer ${HETZNER_TOKEN}" \
"https://api.hetzner.cloud/v1/servers" |
jq -r '.servers | .[] | .public_net.ipv4.ip + "|" + .name'
); do
local ip=$(echo "${iphost}" | cut -d '|' -f 1)
local name=$(echo "${iphost}" | cut -d '|' -f 2)
hosts[${name}]=${ip}
done
}
function get_cloudnode() {
mtr -r -c1 "${1}" | grep -Eo '[0-9]*.your-cloud.host' | cut -d '.' -f 1 | tail -n1 || echo "n/a"
}
function log() {
echo -e "\e[36m[$(date +%H:%M:%S)] $1\e[0m" >&2
}
function main() {
check_env
log "Fetching hosts of account..."
fetch_hosts
log "Finding cloud nodes..."
for name in "${!hosts[@]}"; do
echo -e "Host ${name}\tCloud-Node $(get_cloudnode ${hosts[$name]})"
done
log "All done."
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment