The DigitalOcean metadata endpoint for reserved/floating IPv4 is
/metadata/v1/{reserved,floating}_ip/ipv4/ip_address, not .../address.
The .../active check works but the wrong IP path returned an empty
body, causing the script to silently fall through to ipify on every
droplet with a reserved/floating IP attached.
Verified on a droplet:
curl .../reserved_ip/ipv4/ip_address -> 45.55.111.240
curl .../reserved_ip/ipv4/address -> (empty)
34 lines
856 B
Bash
Executable File
34 lines
856 B
Bash
Executable File
#!/bin/bash
|
|
#
|
|
# Print the "best" IP for the tmux status bar.
|
|
# Preference: DO reserved/floating IP > public egress (ipify) > first local IP.
|
|
#
|
|
|
|
set -u
|
|
|
|
META="http://169.254.169.254/metadata/v1"
|
|
CURL="curl -sf --connect-timeout 1 --max-time 2"
|
|
|
|
is_digitalocean() {
|
|
[ -r /sys/class/dmi/id/sys_vendor ] \
|
|
&& grep -qi digitalocean /sys/class/dmi/id/sys_vendor
|
|
}
|
|
|
|
do_anchor_ip() {
|
|
local kind active ip
|
|
for kind in reserved_ip floating_ip; do
|
|
active=$($CURL "$META/$kind/ipv4/active" 2>/dev/null) || continue
|
|
[ "$active" = "true" ] || continue
|
|
ip=$($CURL "$META/$kind/ipv4/ip_address" 2>/dev/null) || continue
|
|
[ -n "$ip" ] && { printf '%s\n' "$ip"; return 0; }
|
|
done
|
|
return 1
|
|
}
|
|
|
|
if is_digitalocean && do_anchor_ip; then
|
|
exit 0
|
|
fi
|
|
|
|
curl -s --connect-timeout 3 https://api.ipify.org 2>/dev/null \
|
|
|| hostname -I | awk '{print $1}'
|