Sample code for 30+ languages & platforms
Ruby

SSH Through an HTTP Proxy

See more SSH Examples

Demonstrates connecting to an SSH server through an HTTP proxy, using the HTTP/1.1 CONNECT method. Set HttpProxyHostname and HttpProxyPort before calling Connect.

Background: CONNECT asks the proxy to open a raw TCP tunnel rather than to fetch a page, which is how non-HTTP protocols traverse an HTTP proxy. The important caveat is that the proxy must be configured to permit it: many proxies allow CONNECT only to port 443, in which case an SSH connection on port 22 is refused no matter how the client is configured. Typical proxy ports are 3128 and 8080.

Chilkat Ruby Downloads

Ruby
require 'chilkat'

success = false

#  This example requires the Chilkat API to have been previously unlocked.
#  See Global Unlock Sample for sample code.

#  Demonstrates connecting to an SSH server through an HTTP proxy, using the HTTP/1.1 CONNECT
#  method.

ssh = Chilkat::CkSsh.new()

#  Set the proxy hostname (or IP address) and port before connecting.  Typical HTTP proxy ports
#  are 3128 and 8080.
ssh.put_HttpProxyHostname("proxy.example.com")
ssh.put_HttpProxyPort(3128)

#  Important: the HTTP proxy must allow non-HTTP traffic to pass through the CONNECT tunnel,
#  otherwise this cannot work.

hostname = "ssh.example.com"
port = 22
success = ssh.Connect(hostname,port)
if (success == false)
    print ssh.lastErrorText() + "\n";
    exit
end

print "Connected to the SSH server through the HTTP proxy." + "\n";

#  Normally you would not hard-code the password in source.  You should instead obtain it
#  from an interactive prompt, environment variable, or a secrets vault.
password = "mySshPassword"

success = ssh.AuthenticatePw("mySshLogin",password)
if (success == false)
    print ssh.lastErrorText() + "\n";
    exit
end

#  ... use the SSH connection normally ...

ssh.Disconnect()