Skip to content

Instantly share code, notes, and snippets.

@toonetown
Last active August 29, 2017 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toonetown/444211fc2830ae765693f8c28612b6e5 to your computer and use it in GitHub Desktop.
Save toonetown/444211fc2830ae765693f8c28612b6e5 to your computer and use it in GitHub Desktop.
Leverages alias-local to automatically add/remove SSH hosts.
#!/bin/bash
source /etc/profile
# The location of the ssh config file and alias-local
: ${SSHCONF:="/var/run/Shimo/configs/${CS_UNIQUE_IDENTIFIER}.sshconf"}
: ${ALIAS_IFACE:="lo0"}
: ${ALIAS_LOCAL:="$(which alias-local 2>/dev/null)"}
# Read the mappings in the config file - only if we have an alias-local command installed in our path
[ -f "${SSHCONF}" -a -x "${ALIAS_LOCAL}" ] && {
MAPPED=()
for i in $(cat "${SSHCONF}" | grep '^LocalForward ' | cut -d' ' -f2 | cut -d':' -f1 | sort -u); do
"${ALIAS_LOCAL}" "${i}" "${ALIAS_IFACE}" && MAPPED+=(${i})
done
trap 'for i in "${MAPPED[@]}"; do "${ALIAS_LOCAL}" -d "${i}" "${ALIAS_IFACE}" &>/dev/null; done;' EXIT
}
# Call the original parameters as-is
"$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment