Skip to content

Instantly share code, notes, and snippets.

@jaawerth
Last active May 24, 2020 16:53
Show Gist options
  • Save jaawerth/daef5d93540e8ee24a4a575afc30294d to your computer and use it in GitHub Desktop.
Save jaawerth/daef5d93540e8ee24a4a575afc30294d to your computer and use it in GitHub Desktop.
quick script to get the hostname from an alias in .ssh/config
#!/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