Last active
August 29, 2015 14:14
-
-
Save alfaproject/462210f23bf9c0e7a534 to your computer and use it in GitHub Desktop.
OSX CherryTech SSH BMIT alias
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
# Check if the VPN is connected | |
# @param $1 vpn name | |
function _vpn_connected | |
{ | |
scutil --nc status "$1" | head -n 1 | grep -q Connected | |
} | |
# Wait up to 20 seconds for VPN connection | |
# @param $1 vpn name | |
function _vpn_poll_until_connected | |
{ | |
# 200 * 0.1 is 20 seconds (bash doesn't support floats) | |
declare -r max_loops=200 | |
local loops=0 | |
while ! _vpn_connected "$1"; do | |
sleep 0.1 | |
((++loops > max_loops)) && break | |
done | |
((loops <= max_loops)) | |
} | |
# SSH alias to connect to BMIT VPN if needed | |
# @param $1 host | |
function alias_vpn_ssh | |
{ | |
declare -r vpn="CherryTech BMIT" | |
local host=$1 | |
# check if host is in bmit (hosts lookup) | |
if grep -q "10\\.250\\.1.*$host" /etc/hosts; then | |
# check if vpn is not connected | |
if ! _vpn_connected "$vpn"; then | |
# try to connect, and wait until it is connected | |
scutil --nc start "$vpn" | |
if ! _vpn_poll_until_connected "$vpn"; then | |
echo "Couldn't connect to VPN." | |
scutil --nc stop "$vpn" | |
return 1 | |
fi | |
fi | |
fi | |
# forward all arguments to ssh | |
ssh $@ | |
} | |
alias ssh=alias_vpn_ssh |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment