c++ - 使用 system() 执行 ssh 命令

标签 c++ linux ssh

我有一个 C++ 应用程序(在 linux 下),它打开了一个 ssh 反向隧道。这很好用。但是我在关闭连接时遇到了一些麻烦。 当我从命令行启动应用程序时,关闭命令将正常工作,但当我从后台的 cron 作业启动应用程序时,连接不会关闭。

这是我的代码:

打开连接:

std::string cmd = "ssh -o StrictHostKeyChecking=no -f -N -T -M -R 8003:192.168.0.1:8001 -R 8007:192.168.0.1:8005 -R 22222:localhost:22 sshService";
system(cmd.c_str());

要关闭我使用的连接:

std::string cmd = "ssh -T -O exit sshService";
system(cmd.c_str());

我的主机配置是:

Host sshService
    HostName www.addressOfServer.com
    User sshUser
    ControlPersist 4h
    ControlPath ~/.ssh/sshService.ctl
    IdentityFile /home/user/sshUser

是否有一些想法,我做错了什么?或者我如何找出问题的一些想法?

最佳答案

发现问题。看来我必须为控制路径使用 tmpfs 路径.... 我将配置更改为:

Host sshService
    HostName www.addressOfServer.com
    User sshUser
    ControlPersist 4h
    ControlPath /tmp/sshService.ctl
    IdentityFile /home/user/sshUser

关于c++ - 使用 system() 执行 ssh 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55934403/

相关文章:

html - Visual Studio工具箱项不可用

检查定义 GNU C

linux - 如何仅在端口打开时终止在端口上运行的进程?

php - 更改 Apache 配置目录?

python - 读取使用 Python Paramiko SFTPClient.open 方法打开的文件速度很慢

python - 如何从 python 包中启动子模块?

c++ - 获取具有默认值的 map 元素的模板

c++ - 在 C++ 中使用 XGBOOST

c# - 在 C# 中调用 Cygwin GCC DLL 在 malloc 上挂起

linux - Net::SSH::AuthenticationFailed:身份验证失败,用户无效