Last active
May 24, 2020 16:53
-
-
Save jaawerth/daef5d93540e8ee24a4a575afc30294d to your computer and use it in GitHub Desktop.
quick script to get the hostname from an alias in .ssh/config
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
#!/usr/bin/env bash | |
SSH_CONFIG="${SSH_CONFIG:-$HOME/.ssh/config}" | |
error() { >&2 printf "Error: %s\n" "$@"; exit 1; } | |
[ ! -r "$SSH_CONFIG" ] && error "Couldn't read SSH_CONFIG: \"$SSH_CONFIG\"" | |
if ! awk -v tgt="$1" ' | |
tolower($1) ~ /^host$/ { | |
host=$2 | |
} | |
tgt==host && tolower($1) ~ /^hostname$/ { | |
print $2; found=1; exit 0; | |
} | |
END { if (!found) exit 1; }' "$SSH_CONFIG"; | |
then | |
error "Couldn't find hostname for $1" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment