c++ - 在 C++ 中使用 system() 时,如何在命令的输出中输入命令/(SSH 密码)?
问题描述
我的目标是连接到 ssh 服务器并使用 c++ 和系统自动输入命令。运行时:
#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
}
它只输入第一个命令,并且只有在退出 ssh 后才输入密码(作为命令)。
是否可以在 ssh 命令中添加密码(可能使用 popen)?之后,它是否能够将命令输入到 ssh 服务器中?
解决方案
你不能。
但也许你可以使用 std::cin 和 std::cout ,我不推荐。
我假设您正在寻找一个类似于paramiko
python 的库。