【问题标题】:C program that connects to remote host and then executes system commands连接远程主机然后执行系统命令的C程序
【发布时间】:2013-12-31 18:08:24
【问题描述】:

我在网上浏览了有关通过 c 程序执行系统命令的信息,但没有人提到在连接到远程主机后执行命令,例如(此连接提示输入用户密码):

 sprintf(buffer1,"ssh -l %s %s ",userName,hostName);
 system((char*)buffer1);

 //Nothing below this executes because the connection has been established

 sprintf(buffer2,"shasum sfin.exe > t.sha");
 system((char*)buffer2);

一旦连接关闭,程序就会继续执行,有没有简单的方法可以继续执行?

【问题讨论】:

    标签: c ssh terminal


    【解决方案1】:

    您需要使用函数popen 而不是system

    http://linux.die.net/man/3/popen

    它运行一个命令,返回一个文件对象,您可以使用 fprintf、fwrite 等函数写入该文件对象,这些命令将通过 ssh 进程到达远程计算机。

    【讨论】:

      猜你喜欢
      • 2015-05-21
      • 1970-01-01
      • 2020-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-10
      • 2018-02-07
      相关资源
      最近更新 更多