Created
June 1, 2017 05:48
-
-
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
This file contains hidden or 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
#!/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