当前位置: 动力学知识库 > 问答 > 编程问答 >

Using RSA key with chilkat API in C

问题描述:

I tried to use the RSA key from my .ssh directory using chilkat API in C but failed.

Does anyone have such experience?

Here is what I did so far:

#include <stdio.h>

#include "/home/germaneau/opt/chilkat/chilkat-9.5.0-x86-linux-gcc/include/C_CkSsh.h"

#include "/home/germaneau/opt/chilkat/chilkat-9.5.0-x86-linux-gcc/include/C_CkFtp2.h"

#include "/home/germaneau/opt/chilkat/chilkat-9.5.0-x86-linux-gcc/include/C_CkSshKey.h"

void testFtp2Create(void)

{

HCkFtp2 ftp;

BOOL success;

ftp = CkFtp2_Create();

// Any string unlocks the component for the 1st 30-days.

success = CkFtp2_UnlockComponent(ftp,"Anything for 30-day trial");

if (success != TRUE) {

printf("%s\n",CkFtp2_lastErrorText(ftp));

}

CkFtp2_Dispose(ftp);

}

void SSHSample(void)

{

HCkSsh ssh;

BOOL success;

const char * hostname;

long port;

long channelNum;

const char * cmdOutput;

HCkString privKey;

HCkSshKey key;

// Important: It is helpful to send the contents of the

// ssh.LastErrorText property when requesting support.

ssh = CkSsh_Create();

key = CkSshKey_Create();

// Any string automatically begins a fully-functional 30-day trial.

success = CkSsh_UnlockComponent(ssh,"Anything for 30-day trial");

if (success != TRUE) {

printf("%s\n",CkSsh_lastErrorText(ssh));

return;

}

// Connect to an SSH server:

// Hostname may be an IP address or hostname:

hostname = "202.120.32.202";

port = 22;

success = CkSsh_Connect(ssh,hostname,port);

if (success != TRUE) {

printf("%s\n",CkSsh_lastErrorText(ssh));

return;

}

// Wait a max of 5 seconds when reading responses..

CkSsh_putIdleTimeoutMs(ssh,5000);

// load RSA key

success = CkSshKey_LoadText(key,"/home/germaneau/.ssh/id_rsa",privKey);

if (CkSshKey_getIsPrivateKey(key)) printf("PrivateKey\n");

if (CkSshKey_getIsRsaKey(key)) printf("RsaKey\n");

// Authenticate using login/privkey:

success = CkSsh_AuthenticatePk(ssh,"eric",key);

if (success != TRUE) {

printf("%s\n",CkSsh_lastErrorText(ssh));

return;

}

// Open a session channel. (It is possible to have multiple

// session channels open simultaneously.)

channelNum = CkSsh_OpenSessionChannel(ssh);

if (channelNum < 0) {

printf("%s\n",CkSsh_lastErrorText(ssh));

return;

}

// In this example, we'll copy wine.html to wine2.html

// The "cp" command has no output (i.e. nothing is written to

// the standard output) so we'll include an "echo FINISHED"

// so we can programmatically retrieve the output and close

// the channel. Closing the channel immediately after sending

// the command is not good because w/ some SSH servers

// it introduces a race condition where the command may not

// be executed if the server thinks the client has disconnected.

success = CkSsh_SendReqExec(ssh,channelNum,"echo STARTED && hostname && echo FINISHED");

if (success != TRUE) {

printf("%s\n",CkSsh_lastErrorText(ssh));

return;

}

// Read the channel until we receive the FINISHED string.

success = CkSsh_ChannelReceiveUntilMatch(ssh,channelNum,"FINISHED","ansi",TRUE);

if (success != TRUE) {

printf("%s\n",CkSsh_lastErrorText(ssh));

return;

}

// Close the channel:

success = CkSsh_ChannelSendClose(ssh,channelNum);

if (success != TRUE) {

printf("%s\n",CkSsh_lastErrorText(ssh));

return;

}

// Let's pickup the accumulated output of the command:

// (In this case, it will be the string "FINISHED")

cmdOutput = CkSsh_getReceivedText(ssh,channelNum,"ansi");

if (cmdOutput == 0 ) {

printf("%s\n",CkSsh_lastErrorText(ssh));

return;

}

// Display the remote shell's command output:

printf("%s\n",cmdOutput);

// Disconnect

CkSsh_Disconnect(ssh);

CkSsh_Dispose(ssh);

}

int main(int argc, char *argv[])

{

printf("FTP\n");

testFtp2Create();

printf("OK\n");

printf("SSH\n");

SSHSample();

printf("OK\n");

return 0;

}

And here is the output I get:

FTP

OK

SSH

RsaKey

ChilkatLog:

AuthenticatePk:

DllDate: Mar 7 2014

ChilkatVersion: 9.5.0.17

UnlockPrefix: Anything for 30-day trial

Architecture: Little Endian; 32-bit

Language: Linux C/C++

VerboseLogging: 0

SshVersion: SSH-2.0-OpenSSH_5.3

The SSH key object did not contain a loaded private key.

Failed.

--AuthenticatePk

--ChilkatLog

OK

I just don't understand why I get the message

  • The SSH key object did not contain a loaded private key.

Thanks for your help.

分享给朋友:
您可能感兴趣的文章:
随机阅读: