【问题标题】:How to enter commands/(SSH password) within the output of a command when using system() in C++?在 C++ 中使用 system() 时,如何在命令的输出中输入命令/(SSH 密码)?
【发布时间】: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服务器输入命令了吗?

【问题讨论】:

  • 出于安全原因,实施良好的应用不会从标准输入读取密码。相反,他们直接从 tty 读取密码(通常是/dev/tty)。请参阅 expect / pexpect 之类的工具,了解如何操作 tty。
  • 每个system 调用都会启动一个新进程。您无法与该进程通信。
  • 我会使用密钥而不是密码进行自动 SSH 登录
  • 事实是system 使用简单,但功能确实很差。您想要的当然是可能的(即使可能不是以可移植的方式),但不能使用system
  • 你可以使用像libssh这样的库:api.libssh.org/master/libssh_tutor_guided_tour.html

标签: c++ ssh cmd system


【解决方案1】:

不,你不能。

但也许你可以使用 std::cin 和 std::cout ,我不推荐这样做。

我假设您正在寻找一个类似于 paramiko 的库,但它是在 python 中的。

【讨论】:

    猜你喜欢
    • 2023-03-25
    • 2010-12-19
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 2017-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多