Sample code for 30+ languages & platforms
Xbase++

Route Socket Connections through an SSH Object

See more Socket/SSL/TLS Examples

Demonstrates Socket.UseSsh, which configures a socket to route subsequent Connect calls through an already connected and authenticated Ssh object using SSH port forwarding.

Background. After UseSsh, the destination host and port passed to Connect are reached through the SSH tunnel rather than by a direct TCP connection. This is one of several SSH-tunneling approaches the Socket class supports.

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oSsh
LOCAL cSshPassword
LOCAL oSocket
LOCAL nBTls
LOCAL nMaxWaitMs

nSuccess := 0

oSsh := CreateObject("Chilkat.Ssh")

//  Connect and authenticate a standalone SSH object.
nSuccess := oSsh:Connect("ssh.example.com", 22)
IF (nSuccess == 0)
    ? oSsh:LastErrorText
    oSsh:destroy()
    RETURN
ENDIF

//  The SSH password should come from a secure source rather than being hard-coded.
cSshPassword := "mySshPassword"
nSuccess := oSsh:AuthenticatePw("sshUser", cSshPassword)
IF (nSuccess == 0)
    ? oSsh:LastErrorText
    oSsh:destroy()
    RETURN
ENDIF

oSocket := CreateObject("Chilkat.Socket")

//  Route subsequent Connect calls through the already connected and authenticated SSH object.  The
//  destination is reached by SSH port forwarding rather than by a direct TCP connection.
nSuccess := oSocket:UseSsh(oSsh)
IF (nSuccess == 0)
    ? oSocket:LastErrorText
    oSsh:destroy()
    oSocket:destroy()
    RETURN
ENDIF

//  Connect to the destination through the SSH tunnel.
nBTls := 0
nMaxWaitMs := 5000
nSuccess := oSocket:Connect("db.internal", 5432, nBTls, nMaxWaitMs)
IF (nSuccess == 0)
    ? oSocket:LastErrorText
    oSsh:destroy()
    oSocket:destroy()
    RETURN
ENDIF

? "Connected to the destination through the SSH tunnel."

oSsh:destroy()
oSocket:destroy()