Unable to connect to ssh server using system() function in C++

心已入冬 提交于 2021-02-11 12:59:06

问题


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:~$

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 . . .

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?


回答1:


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.



来源:https://stackoverflow.com/questions/63750761/unable-to-connect-to-ssh-server-using-system-function-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!