【发布时间】:2021-11-11 10:47:00
【问题描述】:
我的目标是连接到 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服务器输入命令了吗?
【问题讨论】:
-
每个
system调用都会启动一个新进程。您无法与该进程通信。 -
我会使用密钥而不是密码进行自动 SSH 登录
-
事实是
system使用简单,但功能确实很差。您想要的当然是可能的(即使可能不是以可移植的方式),但不能使用system。 -
你可以使用像
libssh这样的库:api.libssh.org/master/libssh_tutor_guided_tour.html