Sample code for 30+ languages & platforms
Tcl

SSH Tunnel Through an Outbound HTTP Proxy

See more SSH Tunnel Examples

Demonstrates reaching the SSH server through an outbound HTTP proxy using the HttpProxyHostname, HttpProxyPort, HttpProxyUsername, HttpProxyPassword, HttpProxyAuthMethod, and HttpProxyDomain properties.

Background: Many corporate networks force outbound traffic through an HTTP proxy, which reaches non-HTTP services such as SSH via the CONNECT method — so the proxy must be configured to allow CONNECT to the SSH port. HttpProxyAuthMethod is Basic or NTLM, with HttpProxyDomain supplying the Windows domain for NTLM. Note that if SocksVersion is set, the SOCKS proxy takes precedence and these HTTP settings are ignored.

Chilkat Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

#  Demonstrates reaching the SSH server through an outbound HTTP proxy using the SshTunnel
#  properties HttpProxyHostname, HttpProxyPort, HttpProxyUsername, HttpProxyPassword,
#  HttpProxyAuthMethod, and HttpProxyDomain.

set tunnel [new_CkSshTunnel]

#  The HTTP proxy through which the outbound connection to the SSH server is made.  Common HTTP
#  proxy ports include 8080 and 3128.  (If SocksVersion is 4 or 5, the SOCKS proxy takes
#  precedence and these settings are ignored.)
CkSshTunnel_put_HttpProxyHostname $tunnel "proxy.example.com"
CkSshTunnel_put_HttpProxyPort $tunnel 8080

#  Proxy authentication, if the proxy requires it.  Valid HttpProxyAuthMethod values are "Basic"
#  and "NTLM".  HttpProxyDomain is the optional Windows domain used for NTLM.
CkSshTunnel_put_HttpProxyUsername $tunnel "myProxyLogin"
CkSshTunnel_put_HttpProxyPassword $tunnel "myProxyPassword"
CkSshTunnel_put_HttpProxyAuthMethod $tunnel "NTLM"
CkSshTunnel_put_HttpProxyDomain $tunnel "CORP"

CkSshTunnel_put_DestHostname $tunnel "db.internal.example.com"
CkSshTunnel_put_DestPort $tunnel 5432

set sshPort 22
set success [CkSshTunnel_Connect $tunnel "ssh.example.com" $sshPort]
if {$success == 0} then {
    puts [CkSshTunnel_lastErrorText $tunnel]
    delete_CkSshTunnel $tunnel
    exit
}

#  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.
set password "mySshPassword"

set success [CkSshTunnel_AuthenticatePw $tunnel "mySshLogin" $password]
if {$success == 0} then {
    puts [CkSshTunnel_lastErrorText $tunnel]
    delete_CkSshTunnel $tunnel
    exit
}

set listenPort 1080
set success [CkSshTunnel_BeginAccepting $tunnel $listenPort]
if {$success == 0} then {
    puts [CkSshTunnel_lastErrorText $tunnel]
    delete_CkSshTunnel $tunnel
    exit
}

puts "Tunnel established through the HTTP proxy."

set waitForThreadExit 1
set success [CkSshTunnel_CloseTunnel $tunnel $waitForThreadExit]
if {$success == 0} then {
    puts [CkSshTunnel_lastErrorText $tunnel]
    delete_CkSshTunnel $tunnel
    exit
}


delete_CkSshTunnel $tunnel