Last active
July 28, 2021 17:24
-
-
Save jc00ke/06ab088b9f889b551ef3fea7b970b97d to your computer and use it in GitHub Desktop.
Tailscale IP address by hostname with jq
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/env bash | |
set -o pipefail | |
read -r -d '' HELP <<-'HELPDOC' | |
ts-ip-by-hostname — returns the Tailscale IP address of a connected | |
device by the hostname. | |
Usage: | |
ts-ip-by-hostname <hostname> | |
Options: | |
<hostname> hostname to match & return IP. | |
-h Show this message. | |
HELPDOC | |
help() { | |
echo "$HELP" | |
} | |
if [[ $1 == "-h" || -z $1 ]]; then | |
help | |
exit 1 | |
fi | |
hostname="$1" | |
read -r -d '' query <<QUERY | |
.Peer | .[] | select(.HostName == "$hostname") | .TailAddr | |
QUERY | |
ip=$(tailscale status --json | jq -r "$query") | |
if [[ -z $ip ]]; then | |
exit 1 | |
fi | |
echo "$ip" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Even better
tailscale ip -4 <hostname>
If you need all IPs (IPv4 and IPv6) omit the
-4
or-6
.h/t Ross