From 129d77471b13daa8c4c3915ab535c7170fd5d18c Mon Sep 17 00:00:00 2001 From: dissimulo Date: Tue, 21 Apr 2026 07:10:13 -0700 Subject: [PATCH] tmux-ip: prefer DigitalOcean reserved/floating IP when on a droplet If /sys/class/dmi/id/sys_vendor says DigitalOcean and the droplet has a reserved_ip or floating_ip assigned, use that over the ipify-based egress IP. Keeps the ipify/hostname -I fallback intact for other hosts. Gated on the DMI check so non-DO hosts skip the metadata probe entirely and the tmux status bar refresh stays fast. --- .local/bin/tmux-ip | 33 ++++++++++++++++++++++++++++++++- README.md | 5 +++-- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/.local/bin/tmux-ip b/.local/bin/tmux-ip index 929c334c..0f589fca 100755 --- a/.local/bin/tmux-ip +++ b/.local/bin/tmux-ip @@ -1,2 +1,33 @@ #!/bin/bash -curl -s --connect-timeout 3 https://api.ipify.org 2>/dev/null || hostname -I | awk '{print $1}' +# +# 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/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}' diff --git a/README.md b/README.md index c3b74845..78664977 100644 --- a/README.md +++ b/README.md @@ -37,8 +37,9 @@ On top of stock YADR: - **`.tmux.conf`** — real file (not the YADR symlink), with internal/public IP in the status bar and 80k-line scrollback -- **`.local/bin/tmux-ip`** — helper used by `.tmux.conf`; prefers internal IP - with a public-IP fallback +- **`.local/bin/tmux-ip`** — helper used by `.tmux.conf`; prefers a + DigitalOcean reserved/floating IP if the host is a droplet with one + attached, falls back to public egress (ipify) and then first local IP - **`.irssi/`** — IRC client config (no credentials committed) - **`.fonts/`** — Inconsolata, Menlo-Powerline, mensch-Powerline, monof55/56 - **`.themes/`** — BlackMATE v2 / v2.2 / "The Blues" GNOME metathemes