Skip to content

Instantly share code, notes, and snippets.

@brwyatt
Created June 1, 2017 05:48
Show Gist options
  • Save brwyatt/75d07826f05931ed53dd647302dde165 to your computer and use it in GitHub Desktop.
Save brwyatt/75d07826f05931ed53dd647302dde165 to your computer and use it in GitHub Desktop.
Bash script to automatically proxy SSH connections through a proxy host when off-network
#!/bin/bash
# Put the following in the `~/.ssh/config` for each host to
# conditionally proxy:
# ProxyCommand ssh_proxy.sh %h %p bastion.example.net
if [ $# -ne 3 ]; then
echo "USAGE: $0 HOST PORT PROXYHOST" >&2
exit 99
fi
if host "${1}" >/dev/null 2>&1 && \
echo | nc -w1 "${1}" "${2}" 2>&1 | \
egrep '^SSH-[0-9]\.[0-9]' >/dev/null 2>&1; then
# echo "Using netcat" >&2
nc -w35 "${1}" "${2}"
exit $?
else
# echo "Using SSH" >&2
ssh -W "${1}:${2}" "${3}"
exit $?
fi
exit 99
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment