Xbase++
Xbase++
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 Xbase++ Downloads
LOCAL nSuccess
LOCAL oSocket
LOCAL oSshKey
LOCAL cKeyText
nSuccess := 0
oSocket := CreateObject("Chilkat.Socket")
nSuccess := oSocket:SshOpenTunnel("ssh.example.com", 22)
IF (nSuccess == 0)
? oSocket:LastErrorText
oSocket:destroy()
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.
oSshKey := CreateObject("Chilkat.SshKey")
cKeyText := oSshKey:LoadText("qa_data/id_ed25519")
IF (oSshKey:LastMethodSuccess == 0)
? oSshKey:LastErrorText
oSocket:destroy()
oSshKey:destroy()
RETURN
ENDIF
nSuccess := oSshKey:FromOpenSshPrivateKey(cKeyText)
IF (nSuccess == 0)
? oSshKey:LastErrorText
oSocket:destroy()
oSshKey:destroy()
RETURN
ENDIF
// Authenticate the SSH tunnel session using public-key authentication. The SSH server must have the
// corresponding public key authorized for the account.
nSuccess := oSocket:SshAuthenticatePk("sshUser", oSshKey)
IF (nSuccess == 0)
? oSocket:LastErrorText
oSocket:destroy()
oSshKey:destroy()
RETURN
ENDIF
? "SSH public-key authentication succeeded."
oSocket:destroy()
oSshKey:destroy()