PureBasic
PureBasic
Use a PuTTY Key for SSH Authentication
See more SSH Examples
Demonstrates authenticating with an SSH server using a username and a PuTTY (.ppk) private key. The key is imported with FromPuttyPrivateKey — setting Password first if it is encrypted — and passed to AuthenticatePk.
Note: The file paths are relative to the application's current working directory. Supply the paths to your own files.
Background: A PuTTY key can be used directly for authentication with no conversion step, which is convenient when the key was generated by PuTTYgen on Windows. Conceptually the private key takes the place of a password while the username identifies the account, and the corresponding public key must already be installed on the server. Convert to OpenSSH format only if some other tool in your pipeline requires PEM.
Chilkat PureBasic Downloads
IncludeFile "CkSsh.pb"
IncludeFile "CkSshKey.pb"
Procedure ChilkatExample()
success.i = 0
; This example requires the Chilkat API to have been previously unlocked.
; See Global Unlock Sample for sample code.
; Demonstrates authenticating with an SSH server using a username and a PuTTY (.ppk) private
; key. The private key serves as the credential; the username identifies the account on the
; server.
ssh.i = CkSsh::ckCreate()
If ssh.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
; LoadText is a convenience method that reads any text file into a string. It does not itself
; load the key.
puttyKey.i = CkSshKey::ckCreate()
If puttyKey.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
ppkText.s = CkSshKey::ckLoadText(puttyKey,"qa_data/ppk/putty_private_secret.ppk")
If CkSshKey::ckLastMethodSuccess(puttyKey) = 0
Debug CkSshKey::ckLastErrorText(puttyKey)
CkSsh::ckDispose(ssh)
CkSshKey::ckDispose(puttyKey)
ProcedureReturn
EndIf
; Set the password before importing an encrypted .ppk. This should come from a secure source
; rather than being hard-coded.
CkSshKey::setCkPassword(puttyKey, "myKeyPassword")
success = CkSshKey::ckFromPuttyPrivateKey(puttyKey,ppkText)
If success = 0
Debug CkSshKey::ckLastErrorText(puttyKey)
CkSsh::ckDispose(ssh)
CkSshKey::ckDispose(puttyKey)
ProcedureReturn
EndIf
sshHostname.s = "ssh.example.com"
sshPort.i = 22
success = CkSsh::ckConnect(ssh,sshHostname,sshPort)
If success = 0
Debug CkSsh::ckLastErrorText(ssh)
CkSsh::ckDispose(ssh)
CkSshKey::ckDispose(puttyKey)
ProcedureReturn
EndIf
; The corresponding public key must already be installed on the SSH server for the account.
success = CkSsh::ckAuthenticatePk(ssh,"mySshLogin",puttyKey)
If success = 0
Debug CkSsh::ckLastErrorText(ssh)
CkSsh::ckDispose(ssh)
CkSshKey::ckDispose(puttyKey)
ProcedureReturn
EndIf
Debug "Connection and authentication with the SSH server completed."
CkSsh::ckDisconnect(ssh)
CkSsh::ckDispose(ssh)
CkSshKey::ckDispose(puttyKey)
ProcedureReturn
EndProcedure