コードでパスワードを指定せずに、libssh を使用してリモート デバイスにログインしたいと考えています。私はすでにパスワード認証をテストしており、正常に動作します。libssh の主要な認証 API の多くの組み合わせを試しましたが、うまくいかないようです。
キー認証のために、私はこの関数を書きました:
int vs_ssh_authenticate(vsBasestation_t *bs)
{
int auth = -1;
ssh_key pkey, privkey;
#ifdef KEY_AUTHENTICATION
char *password;
password="sample";
auth = ssh_userauth_password(bs->session,NULL,password);
if (auth==SSH_AUTH_ERROR)
{
fprintf(stderr,"Authentication with password failed: %s\n",ssh_get_error(bs->session));
return auth;
}
#else
if(ssh_pki_import_pubkey_file("~/.ssh/key.pub",&pkey) == SSH_OK)
{
if(ssh_pki_import_privkey_file("~/.ssh/key",NULL,
NULL,NULL,&privkey) == SSH_OK)
{
auth = ssh_userauth_publickey(bs->session, NULL, privkey);
}
}
#endif
return auth;
}
現在、クライアント マシンの公開鍵がサーバーの「authorized_keys」ファイルに存在する場合、このメカニズムは機能します。そうでない場合、アクセス拒否応答がサーバーによって送信されます。このプログラムからクライアントの公開鍵をサーバーにコピーするにはどうすればよいですか?
ありがとう。