Skip to content

Instantly share code, notes, and snippets.

@kfatehi
Created August 2, 2012 14:13

Revisions

  1. kfatehi revised this gist Aug 8, 2012. 2 changed files with 15 additions and 22 deletions.
    23 changes: 15 additions & 8 deletions secure_vnc_tunnel.rb
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,11 @@
    #!/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
    @@ -8,22 +15,22 @@ def recently_ran?(secs)
    exit
    else
    `touch #{__FILE__}`
    puts "Deploying pre-tunnel zombie nuke..."
    `ps aux | grep "ssh -L #{ARGV[0]}" | awk '{print $2}'`.split.each do |pid|
    puts "Shooting a zombie in the face."
    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 "Deployment complete. Attempting to create a secure VNC tunnel."
    puts "Ports cleared (hopefully). Attempting to create a secure VNC tunnel."
    fork {
    `ssh -L #{ARGV[0]}:localhost:#{ARGV[2]} -p #{ARGV[3]} -N -f -l #{ARGV[4]} #{ARGV[1]}`
    `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 #{ARGV[1]}:#{ARGV[2]} constructed on localhost:#{ARGV[0]}."
    puts "Secure VNC tunnel to #{HOST_IP}:#{HOST_LOCAL_TUNNEL_PORT} constructed on localhost:#{LOCAL_TUNNEL_PORT}."
    print "Launching VNC"
    3.times do
    5.times do
    sleep 1
    print '.'
    end
    puts
    `open vnc://localhost:#{ARGV[0]}`
    `open vnc://localhost:#{LOCAL_TUNNEL_PORT}`
    puts "VNC launched."
    end
    14 changes: 0 additions & 14 deletions usage.md
    Original file line number Diff line number Diff line change
    @@ -1,14 +0,0 @@
    ```
    $ bash connect.sh
    Deploying pre-tunnel zombie nuke...
    Shooting a zombie in the face.
    Shooting a zombie in the face.
    Shooting a zombie in the face.
    Deployment complete. Attempting to create a secure VNC tunnel.
    Secure VNC tunnel to 192.168.0.5:5901 constructed on localhost:55001.
    Launching VNC...
    VNC launched.
    Entering SSH session (192.168.0.5:22)
    Last login: Thu Aug 2 07:25:17 2012 from localhost
    [keyvan@192.168.0.5 ~]$
    ```
  2. Keyvan created this gist Aug 2, 2012.
    10 changes: 10 additions & 0 deletions connect.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    #!/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
    29 changes: 29 additions & 0 deletions secure_vnc_tunnel.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    #!/usr/bin/env ruby
    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 "Deploying pre-tunnel zombie nuke..."
    `ps aux | grep "ssh -L #{ARGV[0]}" | awk '{print $2}'`.split.each do |pid|
    puts "Shooting a zombie in the face."
    `kill -9 #{pid} > /dev/null 2>&1`
    end
    puts "Deployment complete. Attempting to create a secure VNC tunnel."
    fork {
    `ssh -L #{ARGV[0]}:localhost:#{ARGV[2]} -p #{ARGV[3]} -N -f -l #{ARGV[4]} #{ARGV[1]}`
    }
    puts "Secure VNC tunnel to #{ARGV[1]}:#{ARGV[2]} constructed on localhost:#{ARGV[0]}."
    print "Launching VNC"
    3.times do
    sleep 1
    print '.'
    end
    puts
    `open vnc://localhost:#{ARGV[0]}`
    puts "VNC launched."
    end
    14 changes: 14 additions & 0 deletions usage.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    ```
    $ bash connect.sh
    Deploying pre-tunnel zombie nuke...
    Shooting a zombie in the face.
    Shooting a zombie in the face.
    Shooting a zombie in the face.
    Deployment complete. Attempting to create a secure VNC tunnel.
    Secure VNC tunnel to 192.168.0.5:5901 constructed on localhost:55001.
    Launching VNC...
    VNC launched.
    Entering SSH session (192.168.0.5:22)
    Last login: Thu Aug 2 07:25:17 2012 from localhost
    [keyvan@192.168.0.5 ~]$
    ```