Sample code for 30+ languages & platforms
Lianja

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 Lianja Downloads

Lianja
llSuccess = .F.

loSsh = createobject("CkSsh")

//  Connect and authenticate a standalone SSH object.
llSuccess = loSsh.Connect("ssh.example.com",22)
if (llSuccess = .F.) then
    ? loSsh.LastErrorText
    release loSsh
    return
endif

//  The SSH password should come from a secure source rather than being hard-coded.
lcSshPassword = "mySshPassword"
llSuccess = loSsh.AuthenticatePw("sshUser",lcSshPassword)
if (llSuccess = .F.) then
    ? loSsh.LastErrorText
    release loSsh
    return
endif

loSocket = createobject("CkSocket")

//  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.
llSuccess = loSocket.UseSsh(loSsh)
if (llSuccess = .F.) then
    ? loSocket.LastErrorText
    release loSsh
    release loSocket
    return
endif

//  Connect to the destination through the SSH tunnel.
llBTls = .F.
lnMaxWaitMs = 5000
llSuccess = loSocket.Connect("db.internal",5432,llBTls,lnMaxWaitMs)
if (llSuccess = .F.) then
    ? loSocket.LastErrorText
    release loSsh
    release loSocket
    return
endif

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


release loSsh
release loSocket