Search code examples
c++sshcmdsystem

How to enter commands/(SSH password) within the output of a command when using system() in C++?


My objective is to connect to an ssh server and enter commands automatically using c++ and system. When running:

#include<iostream>
#include<string>
#include<stdlib.h>
using namespace std;

int main()
{   

string user = "user";
string forwarded = "0.tcp.ngrok.io";
string port = "00000";
string command = "ssh "+user+ "@" +forwarded+ " -p "+port; 

system(command.c_str());//connects to ssh server
system("password");//Would like to enter pass within the output of ssh

}

It only enter first command and only after exiting the ssh it enters the password (as a command).

Is it possible to add the password within the ssh command (maybe with popen) ? After that, will it be able to enter commands into the ssh server?


Solution

  • No, you can not.

    But perhaps you can use std::cin and std::cout for that, which I would NOT recommend.

    I assume you are looking for a library similar to paramiko which is, however, in python.