使用libssh C++包装器的Windows路径问题



我在使用libssh c++包装器的Windows文件路径分隔符方面遇到问题libsshp

假设我有以下代码:

#define SSH_NO_CPP_EXCEPTIONS
#include "libssh/libsshpp.hpp"
#include <iostream>
#pragma comment(lib, "ssh")
int main() 
{
ssh::Session session;
int sessionMsg = -1;
std::string host        = "myhost.com";
std::string user        = "username";
std::string idfile      = "%s\.ssh\id_ed25519";
std::string hostkeys    = "ssh-ed25519";
std::string keyExchange = "curve25519-sha256";
session.setOption(SSH_OPTIONS_HOST,                 host.c_str());
session.setOption(SSH_OPTIONS_USER,                 user.c_str());
session.setOption(SSH_OPTIONS_STRICTHOSTKEYCHECK,   (long)0);
session.setOption(SSH_OPTIONS_HOSTKEYS,             hostkeys.c_str());
session.setOption(SSH_OPTIONS_KEY_EXCHANGE,         keyExchange.c_str());
session.setOption(SSH_OPTIONS_ADD_IDENTITY,         idfile.c_str());
std::cout << "Trying to connect to " << host << " with user " << user << "...n";
session.connect();
if (session.isServerKnown() != SSH_SERVER_KNOWN_OK) {
std::cout << "Server unknown.n";
if (session.writeKnownhost() != SSH_OK) {
std::cout << "Unable to write to known_hosts file.n";
}
else {
session.connect();
}
}
sessionMsg = session.userauthPublickeyAuto();
std::string err = session.getError();
if (sessionMsg != SSH_AUTH_SUCCESS) {
if (!err.empty()) {
std::cout << err;
}
std::cout << "Auth failed.";
}
else {
std::cout << err.empty() ? session.getIssueBanner() : err;
}
}

一开始,我只将idfile值设置为id_ed25519,但后来libssh抱怨道:Failed to read private key: C:UsersMyUser/.ssh/id_ed25519(注意切换斜杠(。在将其更改为%s\.ssh\id_ed25519之后,它似乎对连接例程产生了积极影响,但现在我一直陷入(session.writeKnownhost() != SSH_OK)代码部分。

现在,我想知道这是否是由于私钥文件路径出现的相同"切换斜杠"问题,因为显然libssh想要访问C:UsersMyUser.sshknown_hosts,但很可能该路径设置为类似C:UsersMyUser/.ssh/known_hosts的路径。

我的问题是:是否有可能在会话中以某种方式将路径分隔符更改为windows样式,或者我是否在监督或做错了其他事情?

我能够解决添加SSH_OPTIONS_SH_DIR选项并更改私钥和known_hosts路径(现在相对于SSH目录路径(的问题:

// note here: %s will be replaced by libssh with the home directory path
std::string sshDir = "%s//.ssh"; 
std::string idFile = "id_ed25519";
std::string knownHosts = "known_hosts";
// ...
session.setOption(SSH_OPTIONS_USER, user.c_str());
session.setOption(SSH_OPTIONS_SSH_DIR, sshDir.c_str()); // <-- added
// ...

相关内容

  • 没有找到相关文章

最新更新