Sample code for 30+ languages & platforms
PowerBuilder

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

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Ssh
string ls_SshPassword
oleobject loo_Socket
integer li_BTls
integer li_MaxWaitMs

li_Success = 0

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

//  Connect and authenticate a standalone SSH object.
li_Success = loo_Ssh.Connect("ssh.example.com",22)
if li_Success = 0 then
    Write-Debug loo_Ssh.LastErrorText
    destroy loo_Ssh
    return
end if

//  The SSH password should come from a secure source rather than being hard-coded.
ls_SshPassword = "mySshPassword"
li_Success = loo_Ssh.AuthenticatePw("sshUser",ls_SshPassword)
if li_Success = 0 then
    Write-Debug loo_Ssh.LastErrorText
    destroy loo_Ssh
    return
end if

loo_Socket = create oleobject
li_rc = loo_Socket.ConnectToNewObject("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.
li_Success = loo_Socket.UseSsh(loo_Ssh)
if li_Success = 0 then
    Write-Debug loo_Socket.LastErrorText
    destroy loo_Ssh
    destroy loo_Socket
    return
end if

//  Connect to the destination through the SSH tunnel.
li_BTls = 0
li_MaxWaitMs = 5000
li_Success = loo_Socket.Connect("db.internal",5432,li_BTls,li_MaxWaitMs)
if li_Success = 0 then
    Write-Debug loo_Socket.LastErrorText
    destroy loo_Ssh
    destroy loo_Socket
    return
end if

Write-Debug "Connected to the destination through the SSH tunnel."


destroy loo_Ssh
destroy loo_Socket