Sample code for 30+ languages & platforms
PowerBuilder

SSH Authentication using an SSH Certificate

See more SSH Examples

Demonstrates how to authenticate using an SSH certificate.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_SbSshCert
oleobject loo_SbPrivKey
oleobject loo_Key
oleobject loo_Ssh
string ls_Hostname
integer li_Port

li_Success = 0

// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

loo_SbSshCert = create oleobject
li_rc = loo_SbSshCert.ConnectToNewObject("Chilkat.StringBuilder")
if li_rc < 0 then
    destroy loo_SbSshCert
    MessageBox("Error","Connecting to COM object failed")
    return
end if
li_Success = loo_SbSshCert.LoadFile("qa_data/sshCert/user_ecdsa_key-cert.pub","utf-8")
if li_Success = 0 then
    Write-Debug "Failed to load user_ecdsa_key-cert.pub"
    destroy loo_SbSshCert
    return
end if

loo_SbPrivKey = create oleobject
li_rc = loo_SbPrivKey.ConnectToNewObject("Chilkat.StringBuilder")

li_Success = loo_SbPrivKey.LoadFile("qa_data/sshKeys/user_ecdsa_key","utf-8")
if li_Success = 0 then
    Write-Debug "Failed to load user_ecdsa_key"
    destroy loo_SbSshCert
    destroy loo_SbPrivKey
    return
end if

loo_Key = create oleobject
li_rc = loo_Key.ConnectToNewObject("Chilkat.SshKey")

// Provide the password if the user_ecdsa_key is stored in an encrypted format.
loo_Key.Password = "secret"
li_Success = loo_Key.FromOpenSshPrivateKey(loo_SbPrivKey.GetAsString())
if li_Success = 0 then
    Write-Debug loo_Key.LastErrorText
    destroy loo_SbSshCert
    destroy loo_SbPrivKey
    destroy loo_Key
    return
end if

// Indicate that the SSH certificate is to be used for authentication.
// The UseSshCertificate method was added in Chilkat v11.0.0
loo_Key.UseSshCertificate(loo_SbSshCert.GetAsString())

loo_Ssh = create oleobject
li_rc = loo_Ssh.ConnectToNewObject("Chilkat.Ssh")

ls_Hostname = "ssh.example.com"
li_Port = 22
li_Success = loo_Ssh.Connect(ls_Hostname,li_Port)
if li_Success <> 1 then
    Write-Debug loo_Ssh.LastErrorText
    destroy loo_SbSshCert
    destroy loo_SbPrivKey
    destroy loo_Key
    destroy loo_Ssh
    return
end if

li_Success = loo_Ssh.AuthenticatePk("myLogin",loo_Key)
if li_Success <> 1 then
    Write-Debug loo_Ssh.LastErrorText
    destroy loo_SbSshCert
    destroy loo_SbPrivKey
    destroy loo_Key
    destroy loo_Ssh
    return
end if

Write-Debug "Public-Key Authentication using an SSH Certificate was Successful!"


destroy loo_SbSshCert
destroy loo_SbPrivKey
destroy loo_Key
destroy loo_Ssh