无法使用C ++中的system()函数连接到SSH服务器 [英] Unable to connect to ssh server using system() function in C++

查看:76
本文介绍了无法使用C ++中的system()函数连接到SSH服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用我编写的简单C ++程序连接到学校的ssh服务器.我的Windows计算机已安装了OpenSSH Client,并且能够使用命令提示符建立连接.

I'm trying to connect to my school's ssh server using a simple C++ program that I wrote. My windows computer has OpenSSH Client installed and I'm able to establish a connection using the command prompt.

这是通过命令提示符运行的方式:

Here's how it works via the command prompt:

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 ++程序来复制此操作.这是代码.

I was hoping to replicate this action with a simple C++ program I wrote. Here's the code.

#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");
}

不幸的是,当我尝试运行该程序时,得到以下输出:

Unfortunately when I attempt to run this program I get the following output:

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 Client时,才会出现此消息.但这在命令提示符示例中得到了证明.

As far as I understand, this message is only supposed to come if OpenSSH Client isn't installed on my system. But it is as evidenced in the command prompt example.

为什么会这样,有没有办法解决?

Why is this happening and is there a way to fix it?

推荐答案

感谢所有对此问题发表评论和提供帮助的人.原来,这是我为其构建程序的平台的问题.该平台最初设置为x86,但是一旦将其更改为x64,一切都会按预期进行.

Thanks to everyone who commented and helped on this question. Turned out it was an issue of the platform I was building my program for. The platform was initially set to x86 but once I changed it to x64, everything worked as intended.

这篇关于无法使用C ++中的system()函数连接到SSH服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆