📢 Webサイト閉鎖と移転のお知らせ
このWebサイトは2026年9月に閉鎖いたします。
新しい記事は移転先で追加しております。(旧サイトでは記事を追加しておりません)
細 文字列「__FORCETOC__」を「{{#seo: |title={{PAGENAME}} : Exploring Electronics and SUSE Linux | MochiuWiki |keywords=MochiuWiki,Mochiu,Wiki,Mochiu Wiki,Electric Circuit,Electric,pcb,Mathematics,AVR,TI,STMicro,AVR,ATmega,MSP430,STM,Arduino,Xilinx,FPGA,Verilog,HDL,PinePhone,Pine Phone,Raspberry,Raspberry Pi,C,C++,C#,Qt,Qml,MFC,Shell,Bash,Zsh,Fish,SUSE,SLE,Suse Enterprise,Suse Linux,openSUSE,open SUSE,Leap,Linux,uCLnux,Podman,電気回路,電子回路,基板,プリント基板 |description={{PAGENAME}} - 電子回路とSUSE Linuxに関する情報 | This pag… |
|||
| (同じ利用者による、間の5版が非表示) | |||
| 207行目: | 207行目: | ||
#include <libssh/libssh.h> | #include <libssh/libssh.h> | ||
int VerifyKnownsHost(ssh_session | int VerifyKnownsHost(ssh_session my_ssh_session, QString &strErrMsg); | ||
int main(int argc, char *argv[]) | int main(int argc, char *argv[]) | ||
| 214行目: | 214行目: | ||
// SSHセッションの作成 | // SSHセッションの作成 | ||
ssh_session | ssh_session my_ssh_session = ssh_new(); | ||
if (my_ssh_session == NULL) { | if (my_ssh_session == NULL) { | ||
// 作成に失敗した場合 | // 作成に失敗した場合 | ||
| 225行目: | 225行目: | ||
QString port = "<SSHポート番号 例: 22>"; | QString port = "<SSHポート番号 例: 22>"; | ||
ssh_options_set( | ssh_options_set(my_ssh_session, SSH_OPTIONS_HOST, host.toUtf8().data()); | ||
ssh_options_set( | ssh_options_set(my_ssh_session, SSH_OPTIONS_USER, user.toUtf8().data()); | ||
ssh_options_set( | ssh_options_set(my_ssh_session, SSH_OPTIONS_PORT_STR, port.toUtf8().data()); | ||
// SSH接続 | // SSH接続 | ||
int rc = ssh_connect( | int rc = ssh_connect(my_ssh_session); | ||
if (rc != SSH_OK) { | if (rc != SSH_OK) { | ||
// 接続に失敗した場合 | // 接続に失敗した場合 | ||
fprintf(stderr, "Error connecting to host: %s\n", ssh_get_error( | fprintf(stderr, "Error connecting to host: %s\n", ssh_get_error(my_ssh_session)); | ||
ssh_free( | ssh_free(my_ssh_session); | ||
return -1; | return -1; | ||
| 241行目: | 241行目: | ||
// ~/.sshディレクトリ等にあるファイルに記述されているサーバのIDを検証 | // ~/.sshディレクトリ等にあるファイルに記述されているサーバのIDを検証 | ||
QString strErrMsg = ""; | QString strErrMsg = ""; | ||
if(VerifyKnownsHost( | if(VerifyKnownsHost(my_ssh_session, strErrMsg) < 0) | ||
{ | { | ||
fprintf(stderr, "%s\n", strErrMsg.toUtf8().constData(); | fprintf(stderr, "%s\n", strErrMsg.toUtf8().constData(); | ||
if( | if(my_ssh_session != nullptr) | ||
{ | { | ||
ssh_disconnect( | ssh_disconnect(my_ssh_session); | ||
ssh_free( | ssh_free(my_ssh_session); | ||
} | } | ||
| 258行目: | 258行目: | ||
// 秘密鍵のパスフレーズを設定していない場合 | // 秘密鍵のパスフレーズを設定していない場合 | ||
rc = ssh_userauth_privatekey_file( | rc = ssh_userauth_privatekey_file(my_ssh_session, nullptr, private_key_path, nullptr); | ||
// 秘密鍵のパスフレーズを設定している場合 | // 秘密鍵のパスフレーズを設定している場合 | ||
rc = ssh_userauth_privatekey_file( | rc = ssh_userauth_privatekey_file(my_ssh_session, nullptr, private_key_path, "<秘密鍵のパスフレーズ>"); | ||
if (rc != SSH_AUTH_SUCCESS) { | if (rc != SSH_AUTH_SUCCESS) { | ||
| 276行目: | 276行目: | ||
// SSH接続の切断 | // SSH接続の切断 | ||
ssh_disconnect( | ssh_disconnect(my_ssh_session); | ||
// SSHセッションの解放 | // SSHセッションの解放 | ||
ssh_free( | ssh_free(my_ssh_session); | ||
return 0; | return 0; | ||
} | } | ||
int VerifyKnownsHost(ssh_session | int VerifyKnownsHost(ssh_session my_ssh_session, QString &strErrMsg) | ||
{ | { | ||
// Authenticating the server. | // Authenticating the server. | ||
ssh_key srv_pubkey = {}; | ssh_key srv_pubkey = {}; | ||
if(ssh_get_server_publickey( | if(ssh_get_server_publickey(my_ssh_session, &srv_pubkey) < 0) | ||
{ | { | ||
strErrMsg = tr("Failed to get public key."); | strErrMsg = tr("Failed to get public key."); | ||
| 308行目: | 308行目: | ||
} | } | ||
auto state = ssh_session_is_known_server( | auto state = ssh_session_is_known_server(my_ssh_session); | ||
if(state == ssh_known_hosts_e::SSH_KNOWN_HOSTS_OK) | if(state == ssh_known_hosts_e::SSH_KNOWN_HOSTS_OK) | ||
{ // Authentication Successful | { // Authentication Successful | ||
| 356行目: | 356行目: | ||
else | else | ||
{ | { | ||
iRet = ssh_session_update_known_hosts( | iRet = ssh_session_update_known_hosts(my_ssh_session); | ||
if(iRet < 0) | if(iRet < 0) | ||
{ | { | ||
| 383行目: | 383行目: | ||
else | else | ||
{ | { | ||
iRet = ssh_session_update_known_hosts( | iRet = ssh_session_update_known_hosts(my_ssh_session); | ||
if(iRet < 0) | if(iRet < 0) | ||
{ | { | ||
| 404行目: | 404行目: | ||
return 0; | return 0; | ||
} | } </syntaxhighlight> | ||
<br><br> | <br><br> | ||
| 741行目: | 740行目: | ||
int rc = 0; | int rc = 0; | ||
do { | do { | ||
rc = libssh2_session_handshake( | rc = libssh2_session_handshake(m_Session, m_Socket->socketDescriptor()); | ||
} while (rc == LIBSSH2_ERROR_EAGAIN); | } while (rc == LIBSSH2_ERROR_EAGAIN); | ||
| 764行目: | 763行目: | ||
rc = libssh2_userauth_publickey_fromfile(m_Session, | rc = libssh2_userauth_publickey_fromfile(m_Session, | ||
m_User.toUtf8().constData(), | m_User.toUtf8().constData(), | ||
"", // クライアントPCに秘密鍵があり、 | |||
// リモートPC側に公開鍵が設置されている場合 | |||
m_PrivateKey.toUtf8().constData(), | m_PrivateKey.toUtf8().constData(), | ||
m_Passphrase.toUtf8().constData()); | m_Passphrase.isEmpty() ? nullptr : m_Passphrase.toUtf8().constData()); | ||
} while (rc == LIBSSH2_ERROR_EAGAIN); | } while (rc == LIBSSH2_ERROR_EAGAIN); | ||
| 879行目: | 879行目: | ||
{ | { | ||
QCoreApplication a(argc, argv); | QCoreApplication a(argc, argv); | ||
SSHClient client; | // パスワード認証を行う場合 | ||
client | //SSHClient client("<リモートPC側のIPアドレスまたはホスト名>", <リモートPCのポート番号>, | ||
// "<リモートPC側のユーザ名>", "<リモートPC側のユーザパスワード>"); | |||
// 公開鍵認証を行う場合 | |||
SSHClient client("<リモートPC側のIPアドレスまたはホスト名>", <リモートPCのポート番号>, | |||
"<リモートPC側のユーザ名>", | |||
"<公開鍵ファイルのパス>", // クライアントPCに秘密鍵があり、リモートPC側に公開鍵が設置されている場合は空欄 ("") にする | |||
"<秘密鍵ファイルのパス>", | |||
"<秘密鍵のパスフレーズ>"); | |||
client.connectToHost(); | |||
return a.exec(); | return a.exec(); | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
<br><br> | |||
== SCPコマンドの使用例 == | |||
libSSHライブラリおよびlibSSH2ライブラリを使用したSCPコマンドの使用例は、以下に示すページに記載している。<br> | |||
* [[Qtの基礎_-_SCP#libSSHライブラリを使用したSCPコマンドの使用例|libSSHライブラリを使用したSCPコマンドの使用例]] | |||
* [[Qtの基礎_-_SCP#libSSH2ライブラリを使用したSCPコマンドの使用例|libSSH2ライブラリを使用したSCPコマンドの使用例]] | |||
<br><br> | <br><br> | ||
| 998行目: | 1,013行目: | ||
<br><br> | <br><br> | ||
{{#seo: | |||
|title={{PAGENAME}} : Exploring Electronics and SUSE Linux | MochiuWiki | |||
|keywords=MochiuWiki,Mochiu,Wiki,Mochiu Wiki,Electric Circuit,Electric,pcb,Mathematics,AVR,TI,STMicro,AVR,ATmega,MSP430,STM,Arduino,Xilinx,FPGA,Verilog,HDL,PinePhone,Pine Phone,Raspberry,Raspberry Pi,C,C++,C#,Qt,Qml,MFC,Shell,Bash,Zsh,Fish,SUSE,SLE,Suse Enterprise,Suse Linux,openSUSE,open SUSE,Leap,Linux,uCLnux,Podman,電気回路,電子回路,基板,プリント基板 | |||
|description={{PAGENAME}} - 電子回路とSUSE Linuxに関する情報 | This page is {{PAGENAME}} in our wiki about electronic circuits and SUSE Linux | |||
|image=/resources/assets/MochiuLogo_Single_Blue.png | |||
}} | |||
__FORCETOC__ | __FORCETOC__ | ||
[[カテゴリ:Qt]] | [[カテゴリ:Qt]] | ||