Sample code for 30+ languages & platforms
PowerBuilder

SSH Through a SOCKS Proxy

See more SSH Examples

Demonstrates connecting to an SSH server through a SOCKS4 or SOCKS5 proxy. Set the SOCKS properties before calling Connect, and set SocksVersion to match the proxy server.

Background: SOCKS is a general-purpose TCP relay, so unlike an HTTP proxy it is designed from the start to carry arbitrary protocols — usually the smoother option for SSH. The version matters: SOCKS4 supports only a username with no password, while SOCKS5 supports full login/password authentication as well as IPv6 and remote DNS resolution. Note this is the inverse of dynamic port forwarding: here a SOCKS proxy is used to reach the SSH server, rather than SSH providing a SOCKS proxy.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Ssh
string ls_Hostname
integer li_Port
string ls_Password

li_Success = 0

//  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 a SOCKS4 or SOCKS5 proxy.

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

//  Set the SOCKS proxy properties before connecting.  The hostname may be a domain name or an
//  IP address.
loo_Ssh.SocksHostname = "socks.example.com"
loo_Ssh.SocksPort = 1080
loo_Ssh.SocksUsername = "mySocksLogin"
loo_Ssh.SocksPassword = "mySocksPassword"

//  Set the version to match the proxy server: 4 or 5.
//  Note: SOCKS4 supports only a username, with no password.  SOCKS5 supports full
//  login/password authentication.
loo_Ssh.SocksVersion = 5

ls_Hostname = "ssh.example.com"
li_Port = 22
li_Success = loo_Ssh.Connect(ls_Hostname,li_Port)
if li_Success = 0 then
    Write-Debug loo_Ssh.LastErrorText
    destroy loo_Ssh
    return
end if

Write-Debug "Connected to the SSH server through the SOCKS proxy."

//  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_Ssh.AuthenticatePw("mySshLogin",ls_Password)
if li_Success = 0 then
    Write-Debug loo_Ssh.LastErrorText
    destroy loo_Ssh
    return
end if

//  ... use the SSH connection normally ...

loo_Ssh.Disconnect()


destroy loo_Ssh