无法使用 C++ 中的 system() 函数连接到 ssh 服务器
Unable to connect to ssh server using system() function in C++
我正在尝试使用我编写的一个简单的 C++ 程序连接到我学校的 ssh 服务器。我的 windows 计算机安装了 OpenSSH 客户端,我可以使用命令提示符建立连接。
这是通过命令提示符的工作方式:
C:\Users\Username>ssh username@example.server.name
Password:
Last login: Sat Sep 5 15:20:44 2020 from 117.194.240.211
username@example:~$
我希望用我编写的一个简单的 C++ 程序来复制这个动作。这是代码。
#include <iostream>
#include <cstring>
#include <stdlib.h>
using namespace std;
int main()
{
string hostIP;
string username;
string password;
cout << "Welcome to SSH Program" << endl;
cout << "----------------------" << endl;
cout << "\nEnter host ip or name: ";
cin >> hostIP;
cout << "Enter username: ";
cin >> username;
cout << "\nConnecting...\n" << endl;
string composite = "ssh " + username + "@" + hostIP;
char command[100];
strcpy_s(command, composite.c_str());
system(command);
system("pause");
}
不幸的是,当我尝试 运行 这个程序时,我得到以下输出:
Welcome to SSH Program
----------------------
Enter host ip or name: example.server.name
Enter username: username
Connecting...
'ssh' is not recognized as an internal or external command,
operable program or batch file.
Press any key to continue . . .
据我了解,只有在我的系统上未安装 OpenSSH 客户端时才会出现此消息。但这在命令提示符示例中得到了证明。
为什么会发生这种情况,是否有解决方法?
感谢所有对此问题发表评论和提供帮助的人。原来这是我正在为其构建程序的平台的问题。该平台最初设置为 x86,但一旦我将其更改为 x64,一切都按预期工作。
我正在尝试使用我编写的一个简单的 C++ 程序连接到我学校的 ssh 服务器。我的 windows 计算机安装了 OpenSSH 客户端,我可以使用命令提示符建立连接。
这是通过命令提示符的工作方式:
C:\Users\Username>ssh username@example.server.name
Password:
Last login: Sat Sep 5 15:20:44 2020 from 117.194.240.211
username@example:~$
我希望用我编写的一个简单的 C++ 程序来复制这个动作。这是代码。
#include <iostream>
#include <cstring>
#include <stdlib.h>
using namespace std;
int main()
{
string hostIP;
string username;
string password;
cout << "Welcome to SSH Program" << endl;
cout << "----------------------" << endl;
cout << "\nEnter host ip or name: ";
cin >> hostIP;
cout << "Enter username: ";
cin >> username;
cout << "\nConnecting...\n" << endl;
string composite = "ssh " + username + "@" + hostIP;
char command[100];
strcpy_s(command, composite.c_str());
system(command);
system("pause");
}
不幸的是,当我尝试 运行 这个程序时,我得到以下输出:
Welcome to SSH Program
----------------------
Enter host ip or name: example.server.name
Enter username: username
Connecting...
'ssh' is not recognized as an internal or external command,
operable program or batch file.
Press any key to continue . . .
据我了解,只有在我的系统上未安装 OpenSSH 客户端时才会出现此消息。但这在命令提示符示例中得到了证明。
为什么会发生这种情况,是否有解决方法?
感谢所有对此问题发表评论和提供帮助的人。原来这是我正在为其构建程序的平台的问题。该平台最初设置为 x86,但一旦我将其更改为 x64,一切都按预期工作。