📢 Webサイト閉鎖と移転のお知らせ
このWebサイトは2026年9月に閉鎖いたします。
新しい記事は移転先で追加しております。(旧サイトでは記事を追加しておりません)
| 170行目: | 170行目: | ||
== libSSHの使用例 == | == libSSHの使用例 == | ||
以下の例では、公開鍵認証を使用してリモートPCへSSH接続している。<br> | |||
<br> | |||
<syntaxhighlight lang="c++"> | <syntaxhighlight lang="c++"> | ||
#include <QCoreApplication> | #include <QCoreApplication> | ||
#include <QMessageBox> | |||
#include <libssh/libssh.h> | #include <libssh/libssh.h> | ||
int VerifyKnownsHost(ssh_session session); | int VerifyKnownsHost(ssh_session session, QString &strErrMsg); | ||
int main(int argc, char *argv[]) | int main(int argc, char *argv[]) | ||
| 199行目: | 202行目: | ||
int rc = ssh_connect(ssh_session); | int rc = ssh_connect(ssh_session); | ||
if (rc != SSH_OK) { | if (rc != SSH_OK) { | ||
// | // 接続に失敗した場合 | ||
fprintf(stderr, "Error connecting to host: %s\n", ssh_get_error(ssh_session)); | fprintf(stderr, "Error connecting to host: %s\n", ssh_get_error(ssh_session)); | ||
ssh_free(ssh_session); | ssh_free(ssh_session); | ||
| 206行目: | 209行目: | ||
} | } | ||
// | // ~/.sshディレクトリ等にあるファイルに記述されているサーバのIDを検証 | ||
QString strErrMsg = ""; | |||
if(VerifyKnownsHost(ssh_session) < 0) | if(VerifyKnownsHost(ssh_session, strErrMsg) < 0) | ||
{ | { | ||
if(ssh_session != nullptr) | if(ssh_session != nullptr) | ||
| 224行目: | 227行目: | ||
// 秘密鍵のパスフレーズを設定していない場合 | // 秘密鍵のパスフレーズを設定していない場合 | ||
rc = ssh_userauth_privatekey_file( | rc = ssh_userauth_privatekey_file(ssh_session, nullptr, private_key_path, nullptr); | ||
// 秘密鍵のパスフレーズを設定している場合 | // 秘密鍵のパスフレーズを設定している場合 | ||
rc = ssh_userauth_privatekey_file( | rc = ssh_userauth_privatekey_file(ssh_session, nullptr, private_key_path, "<秘密鍵のパスフレーズ>"); | ||
if (rc != SSH_AUTH_SUCCESS) { | if (rc != SSH_AUTH_SUCCESS) { | ||
// | // 認証に失敗した場合 | ||
fprintf(stderr, "Error authenticating with private key: %s\n", ssh_get_error(my_ssh_session)); | fprintf(stderr, "Error authenticating with private key: %s\n", ssh_get_error(my_ssh_session)); | ||
ssh_disconnect(my_ssh_session); | ssh_disconnect(my_ssh_session); | ||