Skip to content

Instantly share code, notes, and snippets.

@alicraigmile
Created October 16, 2014 08:58
Show Gist options
  • Save alicraigmile/29587aca8581a6ab3835 to your computer and use it in GitHub Desktop.
Save alicraigmile/29587aca8581a6ab3835 to your computer and use it in GitHub Desktop.
Decide which proxies to use in ssh on Mac OS X based on your current network settings.
# Usage: add 'ProxyCommand ~/.ssh/proxy %h %p' to your ~/.ssh/config file
#set -x
connect_directly() {
nc -X 5 $1 $2
}
connect_to_host_outside_the_bbc_from_within() {
nc -x "socks-gw.reith.bbc.co.uk:1085" -X 5 $1 $2
}
connect_to_host_inside_the_bbc_from_outwith() {
echo "The host '$1' is inside the BBC network and you appear not to be."
echo "Please connect to BBC Network, Corporate Wifi or the VPN and try again."
exit
}
connect_to_home_net() {
ssh [email protected] exec nc -q0 $1 $2
}
is_local_host() {
[[ "$1" == "localhost" || "$1" == "127.0.0.1" ]]
}
is_host_a_local_vm() {
[[ "$1" =~ "sandbox.dev.bbc.co.uk" || "$1" =~ "sandbox.bbc.co.uk" ]]
}
is_host_on_reith_net() {
[[ "$1" =~ "national.core.bbc.co.uk" || "$1" =~ "gateway.bbc.co.uk" || "$1" =~ "reith.bbc.co.uk" ]]
}
is_host_on_home_net() {
[[ "$1" =~ ".ochiltree.xgusties.com" ]]
}
vpn_connected() {
local status=`scutil --nc status "BBC VPN" | sed -n 1p`
[[ "$status" = "Connected" ]]
}
bbc_on_network() {
NETWORK_LOCATION="$(/usr/sbin/scselect 2>&1 | egrep '^ \* ' | sed 's:.*(\(.*\)):\1:')"
[[ "$NETWORK_LOCATION" = "BBC On Network" ]]
}
function decide_how_to_connect() {
if is_host_on_home_net "$1"; then
connect_to_home_net $1 $2
else
if bbc_on_network || vpn_connected; then
if is_local_host "$1" || is_host_a_local_vm "$1" || is_host_on_reith_net "$1"; then
connect_directly $1 $2
else
connect_to_host_outside_the_bbc_from_within $1 $2
fi
else
if is_host_on_reith_net "$1"; then
connect_to_host_inside_the_bbc_from_outwith $1 $2
else
connect_directly $1 $2
fi
fi
fi
}
decide_how_to_connect $1 $2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment