【发布时间】:2011-10-03 20:47:20
【问题描述】:
我正在尝试通过一个非常小的 C++ 程序调用一些 shell 命令。
“git clone”或“rsync”等需要密码的命令。 例如,由于 git 使用交互式 SSH,我无法为其提供密码。
到目前为止,我的程序如下:
#include <iostream>
#include <string>
std::string ExecuteShellCommand(const std::string& cmd)
{
FILE* pipe = popen(cmd.c_str(), "r");
if (!pipe)
return std::string("ERROR");
char buffer[128];
std::string result = "";
while(!feof(pipe))
{
if(fgets(buffer, 128, pipe) != NULL)
result += buffer;
}
pclose(pipe);
return result;
}
int main()
{
ExecuteShellCommand("git clone ssh://someurl/somerepo.git");
return 0;
}
输出:
ssh_askpass: exec(/usr/libexec/ssh-askpass): 没有那个文件或目录
ssh_askpass: exec(/usr/libexec/ssh-askpass): 没有那个文件或目录
ssh_askpass: exec(/usr/libexec/ssh-askpass): 没有那个文件或目录
有没有办法让进程提示输入密码,就像我直接从命令提示符执行命令一样?
谢谢!
编辑: 理想情况下,我会直接在 Python 或 Shell 中执行此操作,但我的程序需要读取 C++ 中的不同结构(python 绑定有点矫枉过正)所以我为什么要尝试用 C++ 来做。
【问题讨论】:
-
考虑使用 shell 的脚本语言,例如批处理和 sh。
-
@nightcracker:这是我的初衷,但请看我的编辑。