📢 Webサイト閉鎖と移転のお知らせ
このWebサイトは2026年9月に閉鎖いたします。
新しい記事は移転先で追加しております。(旧サイトでは記事を追加しておりません)
| 208行目: | 208行目: | ||
int VerifyKnownsHost(ssh_session my_ssh_session, QString &strErrMsg); | 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[]) | ||
| 235行目: | 236行目: | ||
fprintf(stderr, "Error connecting to host: %s\n", ssh_get_error(my_ssh_session)); | fprintf(stderr, "Error connecting to host: %s\n", ssh_get_error(my_ssh_session)); | ||
ssh_free(my_ssh_session); | ssh_free(my_ssh_session); | ||
return -1; | return -1; | ||
} | } | ||
| 241行目: | 241行目: | ||
// ~/.sshディレクトリ等にあるファイルに記述されているサーバのIDを検証 | // ~/.sshディレクトリ等にあるファイルに記述されているサーバのIDを検証 | ||
QString strErrMsg = ""; | QString strErrMsg = ""; | ||
if(VerifyKnownsHost(my_ssh_session, strErrMsg) < 0) { | if (VerifyKnownsHost(my_ssh_session, strErrMsg) < 0) { | ||
fprintf(stderr, "%s\n", strErrMsg.toUtf8().constData(); | fprintf(stderr, "%s\n", strErrMsg.toUtf8().constData(); | ||
if(my_ssh_session != nullptr) | if (my_ssh_session != nullptr) { | ||
ssh_disconnect(my_ssh_session); | ssh_disconnect(my_ssh_session); | ||
ssh_free(my_ssh_session); | ssh_free(my_ssh_session); | ||
| 270行目: | 269行目: | ||
} | } | ||
// | // SSHセッションを使用して、SCPコマンドを実行 | ||
// | if (SCP(my_ssh_session)) { | ||
// SSH接続の切断 | |||
ssh_disconnect(my_ssh_session); | |||
// SSHセッションの解放 | |||
ssh_free(my_ssh_session); | |||
return -1; | |||
} | |||
// SSH接続の切断 | // SSH接続の切断 | ||
| 287行目: | 293行目: | ||
// Authenticating the server. | // Authenticating the server. | ||
ssh_key srv_pubkey = {}; | ssh_key srv_pubkey = {}; | ||
if(ssh_get_server_publickey(my_ssh_session, &srv_pubkey) < 0) { | 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."); | ||
| 320行目: | 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>" + | ||
| 330行目: | 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); | ||
| 349行目: | 353行目: | ||
else { | else { | ||
iRet = ssh_session_update_known_hosts(my_ssh_session); | iRet = ssh_session_update_known_hosts(my_ssh_session); | ||
if(iRet < 0) { | if (iRet < 0) { | ||
strErrMsg = tr("Failed to update host key."); | strErrMsg = tr("Failed to update host key."); | ||
return -1; | return -1; | ||
| 387行目: | 391行目: | ||
return 0; | 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); | |||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||