Skip to content

Instantly share code, notes, and snippets.

@kousu
Created May 27, 2015 22:30
Show Gist options
  • Save kousu/ad2e9ac6713671b886a8 to your computer and use it in GitHub Desktop.
Save kousu/ad2e9ac6713671b886a8 to your computer and use it in GitHub Desktop.
automagic VPNing into Windows machines, via ssh
#!/bin/sh
# automagic VPNing into Windows machines, via ssh
# this assumes the machine you're VPNing into also runs an ssh server, which admittedly is an unusual configuration (it could be stretched to not do this)
# we knock on the RDP port; if it doesn't respond, assume we're outside of the firewall, and construct a tunnel
#XXX -g workarea *does not work* with i3. Much sadness.
HOST=${HOST:=XXXXXXXXXXXXXXXXXXX}
PORT=${PORT:=3389}
TIMEOUT=${TIMEOUT:=5}
USER=XXXXXXXXXXXXX
PASS=XXXXXXXXXXXXX
Ctl_Sock=.tunnel.sock
# (by connecting to /dev/null we ensure the connection terminates immediately upon connection, with the only bit of information coming out in $?)
if ! nc -w "$TIMEOUT" "$HOST" "$PORT" < /dev/null > /dev/null; then
# the ssh -M/-S/-O tip to control is from http://www.gossamer-threads.com/lists/openssh/dev/48033 (except they didn't mention the -M flag... maybe it's a new requirements)
ssh -v -N -f -M -S "$Ctl_Sock" -L "$PORT:localhost:$PORT" "$HOST"
HOST=localhost
fi
rdesktop -D -g workarea -u $USER -p - $HOST <<< $PASS
#echo "killing ssh" #DEBUG
if [ -S "$Ctl_Sock" ]; then
ssh -S "$Ctl_Sock" -O exit localhost #'localhost' is just because ssh's command line parser dies without a target before ssh figures out it isn't actually sshing
fi
#pgrep -la ssh #DEBUG
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment