Windows 使用 libssh C++ 包装器的路径问题
Windows path problems using libssh C++ wrapper
我在使用 libssh c++ 包装程序 Windows 文件路径分隔符时遇到问题 libsshpp.
假设我有以下代码:
#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:\Users\MyUser/.ssh/id_ed25519
(注意切换斜杠)。改成%s\.ssh\id_ed25519
后貌似对连接程序产生了积极的影响,然而现在老是掉进(session.writeKnownhost() != SSH_OK)
代码部分。
现在,我想知道这是否可能是由于私钥文件路径出现的相同 "switching slashes" 问题造成的,因为显然 libssh 想要访问 C:\Users\MyUser\.ssh\known_hosts
但很可能路径是设置为 C:\Users\MyUser/.ssh/known_hosts
.
我的问题是:是否有可能在会话中以某种方式将路径分隔符更改为 windows 样式,或者我在这里监督或做错了什么?
我能够解决添加 SSH_OPTIONS_SSH_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
// ...
我在使用 libssh c++ 包装程序 Windows 文件路径分隔符时遇到问题 libsshpp.
假设我有以下代码:
#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:\Users\MyUser/.ssh/id_ed25519
(注意切换斜杠)。改成%s\.ssh\id_ed25519
后貌似对连接程序产生了积极的影响,然而现在老是掉进(session.writeKnownhost() != SSH_OK)
代码部分。
现在,我想知道这是否可能是由于私钥文件路径出现的相同 "switching slashes" 问题造成的,因为显然 libssh 想要访问 C:\Users\MyUser\.ssh\known_hosts
但很可能路径是设置为 C:\Users\MyUser/.ssh/known_hosts
.
我的问题是:是否有可能在会话中以某种方式将路径分隔符更改为 windows 样式,或者我在这里监督或做错了什么?
我能够解决添加 SSH_OPTIONS_SSH_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
// ...