Skip to content

Instantly share code, notes, and snippets.

@jc00ke
Last active July 28, 2021 17:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jc00ke/06ab088b9f889b551ef3fea7b970b97d to your computer and use it in GitHub Desktop.
Save jc00ke/06ab088b9f889b551ef3fea7b970b97d to your computer and use it in GitHub Desktop.
Tailscale IP address by hostname with jq
#!/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"
@jc00ke
Copy link
Author

jc00ke commented Jul 28, 2021

Even better tailscale ip -4 <hostname>

If you need all IPs (IPv4 and IPv6) omit the -4 or -6.

h/t Ross

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment