-
-
Save synsa/5c95642dbd1132d08eb31fdb338c9e17 to your computer and use it in GitHub Desktop.
Secure SSH VNC Tunnel Bash/Ruby Script on Mac OS X
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 | |
TARGET_IP="xx.xx.xx.xx" | |
TARGET_LOCAL_VNC_PORT=5901 | |
LOCAL_VNC_PORT=31543 | |
TARGET_WAN_SSH_PORT=22345 | |
TARGET_USER="keyvan" | |
ruby secure_vnc_tunnel.rb $LOCAL_VNC_PORT $TARGET_IP $TARGET_LOCAL_VNC_PORT $TARGET_WAN_SSH_PORT $TARGET_USER | |
echo "Entering SSH session ($TARGET_IP:$TARGET_WAN_SSH_PORT)" | |
ssh -p $TARGET_WAN_SSH_PORT $TARGET_USER@$TARGET_IP |
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
#!/usr/bin/env ruby | |
LOCAL_TUNNEL_PORT = ARGV[0] | |
HOST_IP = ARGV[1] | |
HOST_LOCAL_TUNNEL_PORT = ARGV[2] | |
HOST_WAN_SSH_PORT = ARGV[3] | |
HOST_USER = ARGV[4] | |
def recently_ran?(secs) | |
File.open(__FILE__, "r+") {|f| @timestamp = f.mtime } | |
(Time.now - @timestamp) < secs | |
end | |
if recently_ran?(5) | |
puts "Recently built tunnel and VNC'd, skipping that." | |
exit | |
else | |
`touch #{__FILE__}` | |
puts "Killing port-stealing stale sessions" | |
`ps aux | grep "ssh -L #{LOCAL_TUNNEL_PORT}" | awk '{print $2}'`.split.each do |pid| | |
`kill -9 #{pid} > /dev/null 2>&1` | |
puts "Killed stale process #{pid}" | |
end | |
puts "Ports cleared (hopefully). Attempting to create a secure VNC tunnel." | |
fork { | |
`ssh -L #{LOCAL_TUNNEL_PORT}:localhost:#{HOST_LOCAL_TUNNEL_PORT} -p #{HOST_WAN_SSH_PORT} -N -f -l #{HOST_USER} #{HOST_IP}` | |
} | |
puts "Secure VNC tunnel to #{HOST_IP}:#{HOST_LOCAL_TUNNEL_PORT} constructed on localhost:#{LOCAL_TUNNEL_PORT}." | |
print "Launching VNC" | |
5.times do | |
sleep 1 | |
print '.' | |
end | |
puts | |
`open vnc://localhost:#{LOCAL_TUNNEL_PORT}` | |
puts "VNC launched." | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment