Sample code for 30+ languages & platforms
Lianja

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

Lianja
llSuccess = .F.

loSocket = createobject("CkSocket")

llSuccess = loSocket.SshOpenTunnel("ssh.example.com",22)
if (llSuccess = .F.) then
    ? loSocket.LastErrorText
    release loSocket
    return
endif

//  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.
loSshKey = createobject("CkSshKey")
lcKeyText = loSshKey.LoadText("qa_data/id_ed25519")
if (loSshKey.LastMethodSuccess = .F.) then
    ? loSshKey.LastErrorText
    release loSocket
    release loSshKey
    return
endif

llSuccess = loSshKey.FromOpenSshPrivateKey(lcKeyText)
if (llSuccess = .F.) then
    ? loSshKey.LastErrorText
    release loSocket
    release loSshKey
    return
endif

//  Authenticate the SSH tunnel session using public-key authentication.  The SSH server must have the
//  corresponding public key authorized for the account.
llSuccess = loSocket.SshAuthenticatePk("sshUser",loSshKey)
if (llSuccess = .F.) then
    ? loSocket.LastErrorText
    release loSocket
    release loSshKey
    return
endif

? "SSH public-key authentication succeeded."


release loSocket
release loSshKey