Sample code for 30+ languages & platforms
PowerBuilder

SSH Tunnel Through an Outbound SOCKS Proxy

See more SSH Tunnel Examples

Demonstrates reaching the SSH server through an outbound SOCKS proxy using the SocksVersion, SocksHostname, SocksPort, SocksUsername, and SocksPassword properties.

Background: This is the reverse direction from dynamic forwarding: here a SOCKS proxy is used to reach the SSH server, for networks where outbound connections must pass through one. SocksVersion is the switch — 0 means no SOCKS, while 4 or 5 selects the protocol. SOCKS5 supports username/password authentication and remote DNS; SOCKS4 supports only a username.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Tunnel
integer li_SshPort
string ls_Password
integer li_ListenPort
integer li_WaitForThreadExit

li_Success = 0

//  Demonstrates reaching the SSH server through an outbound SOCKS proxy using the SshTunnel
//  properties SocksVersion, SocksHostname, SocksPort, SocksUsername, and SocksPassword.

loo_Tunnel = create oleobject
li_rc = loo_Tunnel.ConnectToNewObject("Chilkat.SshTunnel")
if li_rc < 0 then
    destroy loo_Tunnel
    MessageBox("Error","Connecting to COM object failed")
    return
end if

//  Set SocksVersion to 4 or 5 to route the outbound connection to the SSH server through a SOCKS
//  proxy.  A value of 0 (the default) means no SOCKS proxy.
loo_Tunnel.SocksVersion = 5
loo_Tunnel.SocksHostname = "socks.example.com"
loo_Tunnel.SocksPort = 1080

//  SOCKS4 supports only a username; SOCKS5 supports username and password.
loo_Tunnel.SocksUsername = "myProxyLogin"
loo_Tunnel.SocksPassword = "myProxyPassword"

loo_Tunnel.DestHostname = "db.internal.example.com"
loo_Tunnel.DestPort = 5432

//  Connect to the SSH server through the SOCKS proxy configured above.
li_SshPort = 22
li_Success = loo_Tunnel.Connect("ssh.example.com",li_SshPort)
if li_Success = 0 then
    Write-Debug loo_Tunnel.LastErrorText
    destroy loo_Tunnel
    return
end if

//  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.
ls_Password = "mySshPassword"

li_Success = loo_Tunnel.AuthenticatePw("mySshLogin",ls_Password)
if li_Success = 0 then
    Write-Debug loo_Tunnel.LastErrorText
    destroy loo_Tunnel
    return
end if

li_ListenPort = 1080
li_Success = loo_Tunnel.BeginAccepting(li_ListenPort)
if li_Success = 0 then
    Write-Debug loo_Tunnel.LastErrorText
    destroy loo_Tunnel
    return
end if

Write-Debug "Tunnel established through the SOCKS proxy."

li_WaitForThreadExit = 1
li_Success = loo_Tunnel.CloseTunnel(li_WaitForThreadExit)
if li_Success = 0 then
    Write-Debug loo_Tunnel.LastErrorText
    destroy loo_Tunnel
    return
end if



destroy loo_Tunnel