Sample code for 30+ languages & platforms
PowerBuilder

Authenticate an SSH Tunnel with a Private Key

See more Socket/SSL/TLS Examples

Demonstrates Socket.SshAuthenticatePk, which authenticates an SSH tunnel session using a private key loaded into an SshKey object. Call SshOpenTunnel first.

The file path is relative to the application's current working directory. An absolute path may also be used. Supply the path appropriate to your own environment.

Background. The SSH server must have the corresponding public key authorized for the account. If the private key is encrypted, set the SshKey.Password property before loading it.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Socket
oleobject loo_SshKey
string ls_KeyText

li_Success = 0

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

li_Success = loo_Socket.SshOpenTunnel("ssh.example.com",22)
if li_Success = 0 then
    Write-Debug loo_Socket.LastErrorText
    destroy loo_Socket
    return
end if

//  Load the SSH private key.  The file path is relative to the application's current working
//  directory.  If the key is encrypted, set the SshKey.Password property before calling
//  FromOpenSshPrivateKey.
loo_SshKey = create oleobject
li_rc = loo_SshKey.ConnectToNewObject("Chilkat.SshKey")

ls_KeyText = loo_SshKey.LoadText("qa_data/id_ed25519")
if loo_SshKey.LastMethodSuccess = 0 then
    Write-Debug loo_SshKey.LastErrorText
    destroy loo_Socket
    destroy loo_SshKey
    return
end if

li_Success = loo_SshKey.FromOpenSshPrivateKey(ls_KeyText)
if li_Success = 0 then
    Write-Debug loo_SshKey.LastErrorText
    destroy loo_Socket
    destroy loo_SshKey
    return
end if

//  Authenticate the SSH tunnel session using public-key authentication.  The SSH server must have the
//  corresponding public key authorized for the account.
li_Success = loo_Socket.SshAuthenticatePk("sshUser",loo_SshKey)
if li_Success = 0 then
    Write-Debug loo_Socket.LastErrorText
    destroy loo_Socket
    destroy loo_SshKey
    return
end if

Write-Debug "SSH public-key authentication succeeded."


destroy loo_Socket
destroy loo_SshKey