📢 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 SCP(ssh_session my_ssh_session); | |||
int main(int argc, char *argv[]) | int main(int argc, char *argv[]) | ||
| 214行目: | 215行目: | ||
// SSHセッションの作成 | // SSHセッションの作成 | ||
ssh_session | ssh_session my_ssh_session = ssh_new(); | ||
if (my_ssh_session == NULL) { | if (my_ssh_session == NULL) { | ||
// | // SSHセッションの作成に失敗した場合 | ||
return -1; | return -1; | ||
} | } | ||
| 225行目: | 226行目: | ||
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(my_ssh_session); | |||
ssh_disconnect( | ssh_free(my_ssh_session); | ||
ssh_free( | |||
} | } | ||
| 258行目: | 256行目: | ||
// 秘密鍵のパスフレーズを設定していない場合 | // 秘密鍵のパスフレーズを設定していない場合 | ||
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) { | ||
| 271行目: | 269行目: | ||
} | } | ||
// | // SSHセッションを使用して、SCPコマンドを実行 | ||
// | if (SCP(my_ssh_session)) { | ||
// SSH接続の切断 | |||
ssh_disconnect(my_ssh_session); | |||
// SSHセッションの解放 | |||
ssh_free(my_ssh_session); | |||
return -1; | |||
} | |||
// 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."); | ||
| 301行目: | 305行目: | ||
ssh_key_free(srv_pubkey); | ssh_key_free(srv_pubkey); | ||
if(iRet < 0) | if (iRet < 0) { | ||
strErrMsg = tr("Failed to get public key hash."); | strErrMsg = tr("Failed to get public key hash."); | ||
return -1; | return -1; | ||
} | } | ||
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 | |||
} | } | ||
else if(state == ssh_known_hosts_e::SSH_KNOWN_HOSTS_CHANGED) | else if (state == ssh_known_hosts_e::SSH_KNOWN_HOSTS_CHANGED) { | ||
QString strHexa = ssh_get_hexa(hash, hlen); | QString strHexa = ssh_get_hexa(hash, hlen); | ||
| 325行目: | 326行目: | ||
return -1; | return -1; | ||
} | } | ||
else if(state == ssh_known_hosts_e::SSH_KNOWN_HOSTS_OTHER) | else if (state == ssh_known_hosts_e::SSH_KNOWN_HOSTS_OTHER) { | ||
strErrMsg = tr("The host key for this server was not found but an other type of key exists.") + "<br>" + | strErrMsg = tr("The host key for this server was not found but an other type of key exists.") + "<br>" + | ||
tr("An attacker might change the default server key to confuse your client into") + "<br>" + | tr("An attacker might change the default server key to confuse your client into") + "<br>" + | ||
| 335行目: | 335行目: | ||
return -1; | return -1; | ||
} | } | ||
else if(state == ssh_known_hosts_e::SSH_KNOWN_HOSTS_NOT_FOUND) | else if (state == ssh_known_hosts_e::SSH_KNOWN_HOSTS_NOT_FOUND) { | ||
/* FALL THROUGH to SSH_KNOWN_HOSTS_UNKNOWN behavior */ | /* FALL THROUGH to SSH_KNOWN_HOSTS_UNKNOWN behavior */ | ||
QString strHexa = ssh_get_hexa(hash, hlen); | QString strHexa = ssh_get_hexa(hash, hlen); | ||
| 348行目: | 347行目: | ||
ssh_clean_pubkey_hash(&hash); | ssh_clean_pubkey_hash(&hash); | ||
if(ret == QMessageBox::No) | if(ret == QMessageBox::No) { | ||
strErrMsg = tr("To connect, please add host key."); | strErrMsg = tr("To connect, please add host key."); | ||
return -1; | return -1; | ||
} | } | ||
else | else { | ||
iRet = ssh_session_update_known_hosts(my_ssh_session); | |||
iRet = ssh_session_update_known_hosts( | if (iRet < 0) { | ||
if(iRet < 0) | |||
strErrMsg = tr("Failed to update host key."); | strErrMsg = tr("Failed to update host key."); | ||
return -1; | return -1; | ||
} | } | ||
} | } | ||
} | } | ||
else if(state == ssh_known_hosts_e::SSH_KNOWN_HOSTS_UNKNOWN) | else if (state == ssh_known_hosts_e::SSH_KNOWN_HOSTS_UNKNOWN) { | ||
QString strHexa = ssh_get_hexa(hash, hlen); | QString strHexa = ssh_get_hexa(hash, hlen); | ||
QString strAddHostMessage = tr("The server is unknown. Do you trust the host key?") + "\n" + | QString strAddHostMessage = tr("The server is unknown. Do you trust the host key?") + "\n" + | ||
| 375行目: | 368行目: | ||
ssh_clean_pubkey_hash(&hash); | ssh_clean_pubkey_hash(&hash); | ||
if(msgRet == QMessageBox::No) | if (msgRet == QMessageBox::No) { | ||
strErrMsg = tr("To connect, please add host key."); | strErrMsg = tr("To connect, please add host key."); | ||
return -1; | return -1; | ||
} | } | ||
else | else { | ||
iRet = ssh_session_update_known_hosts(my_ssh_session); | |||
iRet = ssh_session_update_known_hosts( | if (iRet < 0) { | ||
if(iRet < 0) | |||
strErrMsg = tr("Failed to update host key."); | strErrMsg = tr("Failed to update host key."); | ||
return -1; | return -1; | ||
} | } | ||
} | } | ||
} | } | ||
else if(state == ssh_known_hosts_e::SSH_KNOWN_HOSTS_ERROR) | else if (state == ssh_known_hosts_e::SSH_KNOWN_HOSTS_ERROR) { | ||
strErrMsg = tr("There was an error in checking the host."); | strErrMsg = tr("There was an error in checking the host."); | ||
| 402行目: | 389行目: | ||
ssh_clean_pubkey_hash(&hash); | ssh_clean_pubkey_hash(&hash); | ||
return 0; | |||
} | |||
int SCP(ssh_session my_ssh_session) | |||
{ | |||
// SCPセッションの作成 | |||
ssh_scp scp = ssh_scp_new(my_ssh_session, SSH_SCP_WRITE, "<送信先のリモートPCのパス>"); | |||
if (scp == NULL) { | |||
fprintf(stderr, "SCPセッションの作成に失敗: %s\n", ssh_get_error(my_ssh_session)); | |||
return -1; | |||
} | |||
// SCPセッションの初期化 | |||
rc = ssh_scp_init(scp); | |||
if (rc != SSH_OK) { | |||
fprintf(stderr, "SCPセッションの作成の初期化に失敗: %s\n", ssh_get_error(my_ssh_session)); | |||
ssh_scp_free(scp); | |||
return -1; | |||
} | |||
// ローカルPC上のファイルをオープン | |||
QFile file("<送信元のファイルパス>"); | |||
if (!file.open(QIODevice::ReadOnly)) { | |||
fprintf(stderr, "ファイルのオープンに失敗\n"); | |||
ssh_scp_free(scp); | |||
return -1; | |||
} | |||
// ファイル内容を読み込む | |||
QByteArray fileContent = file.readAll(); | |||
// ファイルの転送 | |||
rc = ssh_scp_push_file(scp, filename, fileContent.size(), S_IRUSR | S_IWUSR); | |||
if (rc != SSH_OK) { | |||
fprintf(stderr, "送信先のファイルのオープンに失敗: %s\n", ssh_get_error(my_ssh_session)); | |||
ssh_scp_free(scp); | |||
return -1; | |||
} | |||
rc = ssh_scp_write(scp, fileContent.constData(), fileContent.size()); | |||
if (rc != SSH_OK) { | |||
fprintf(stderr, "送信先のファイルの書き込みに失敗: %s\n", ssh_get_error(my_ssh_session)); | |||
ssh_scp_free(scp); | |||
return -1; | |||
} | |||
// SCPセッションの終了 | |||
ssh_scp_close(scp); | |||
ssh_scp_free(scp); | |||
return 0; | return 0; | ||
| 408行目: | 445行目: | ||
<br><br> | <br><br> | ||
== | == libSSH2ライブラリを使用したSCPコマンドの使用例 == | ||
==== ブロッキングモード ==== | |||
以下の例では、リモート側のPCにSSH接続して、SCPでファイルを送信している。<br> | 以下の例では、リモート側のPCにSSH接続して、SCPでファイルを送信している。<br> | ||
<br> | <br> | ||
以下の例は、<u> | 以下の例は、<u>ブロッキングモード</u>でSCPを実行している。<br> | ||
ノンブロッキングモードを有効にする場合、libSSH2ライブラリの関数呼び出しは即座に返されて、処理がバックグラウンドで非同期に進行する。<br> | ノンブロッキングモードを有効にする場合、libSSH2ライブラリの関数呼び出しは即座に返されて、処理がバックグラウンドで非同期に進行する。<br> | ||
これにより、プログラムは他の処理を続行でき、必要に応じてリモートホストとの通信が完了するのを待つことができる。<br> | これにより、プログラムは他の処理を続行でき、必要に応じてリモートホストとの通信が完了するのを待つことができる。<br> | ||
| 833行目: | 871行目: | ||
<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]] | ||