12,964
回編集
418行目: | 418行目: | ||
<br><br> | <br><br> | ||
== | == libSSH2ライブラリを使用したコマンドの使用例 == | ||
以下の例では、リモート側のPCにSSH接続して、lsコマンドを実行してその結果をローカル側のPCに表示している。<br> | |||
<br> | <br> | ||
以下の例は、<u>ノンブロッキングモード</u>でSCPを実行している。<br> | 以下の例は、<u>ノンブロッキングモード</u>でSCPを実行している。<br> | ||
473行目: | 473行目: | ||
#include <QDebug> | #include <QDebug> | ||
#include <libssh2.h> | #include <libssh2.h> | ||
#include "DivideByZeroException.h" | #include "DivideByZeroException.h" | ||
498行目: | 497行目: | ||
int Send(QTcpSocket &sock, LIBSSH2_SESSION *session); | int Send(QTcpSocket &sock, LIBSSH2_SESSION *session); | ||
#endif | #endif | ||
int main(int argc, char *argv[]) | int main(int argc, char *argv[]) | ||
{ | { | ||
QCoreApplication a(argc, argv); | QCoreApplication a(argc, argv); | ||
const char *fingerprint; | |||
LIBSSH2_SESSION *session; | |||
LIBSSH2_CHANNEL *channel; | |||
char *userauth_list; | |||
// | // libSSH2オブジェクトの初期化 | ||
rc = libssh2_init(0); | auto rc = libssh2_init(0); | ||
if (rc != 0) { | if (rc != 0) { | ||
qDebug() << "libssh2 initialization failed"; | |||
return -1; | |||
} | } | ||
// QTcpSocketクラスのインスタンスを生成して、リモートPCに接続 | |||
// | QTcpSocket socket; | ||
socket.connectToHost(<リモートPCのIPアドレスまたはホスト名>, <リモートPCにSSH接続するポート番号>); | |||
// 最大10[秒]待機 | |||
if (!socket.waitForConnected(10000)) { | |||
qDebug() << "SSH接続に失敗:" << socket.errorString(); | |||
return -1; | return -1; | ||
} | } | ||
// セッションの初期化 | |||
session = libssh2_session_init(); | |||
if (libssh2_session_handshake(session, socket.socketDescriptor()) != 0) { | |||
qDebug() << "SSHセッションの確立に失敗"; | |||
if ( | |||
qDebug() << | |||
return -1; | return -1; | ||
} | } | ||
// リモートPCのフィンガープリントを確認 | |||
fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1); | |||
qDebug() << "リモートPCのフィンガープリント:" << fingerprint; | |||
// | // 認証方法の確認 | ||
session | userauth_list = libssh2_userauth_list(session, USERNAME, strlen(USERNAME)); | ||
qDebug() << "認証方法:" << userauth_list; | |||
// パスワード認証を行う場合 | |||
//if (libssh2_userauth_password(session, "<リモートPCのユーザ名>", "<リモートPCのユーザパスワード>") != 0) { | |||
// qDebug() << "パスワードによる認証に失敗"; | |||
// return -1; | |||
//} | |||
// 公開鍵認証 | |||
if (libssh2_userauth_publickey_fromfile(session, "<リモートPCのユーザ名>", "<公開鍵ファイルのパス>", "<秘密鍵ファイルのパス>", "<秘密鍵のパスフレーズ>") != 0) { | |||
qDebug() << "公開鍵認証に失敗"; | |||
return -1; | return -1; | ||
} | } | ||
// チャンネルをオープン | |||
channel = libssh2_channel_open_session(session); | |||
if ( | if (!channel) { | ||
qDebug() << " | qDebug() << "チャンネルのオープンに失敗"; | ||
return -1; | return -1; | ||
} | } | ||
// リモートPC上でls -alコマンドを実行 | |||
if (libssh2_channel_exec(channel, "ls -la") != 0) { | |||
qDebug() << "コマンドの実行に失敗"; | |||
return -1; | return -1; | ||
} | } | ||
// ls -alコマンドの実行結果 | |||
char buffer[1024] = {}; | |||
while ((rc = libssh2_channel_read(channel, buffer, sizeof(buffer))) > 0) { | |||
qDebug().noquote() << QString::fromUtf8(buffer, rc); | |||
} | } | ||
// チャンネルをクローズ | |||
libssh2_channel_free(channel); | |||
// | // セッションのクローズ | ||
libssh2_session_disconnect(session, "Normal Shutdown"); | |||
libssh2_session_free(session); | |||
// ソケットのクローズ | |||
socket.disconnectFromHost(); | |||
// | // libssh2の初期化を解除 | ||
libssh2_exit(); | |||
return | return a.exec(); | ||
} | } | ||