[英]C++ shell command prompting for password
I am trying to call some shell commands via a very small C++ program. 我试图通过一个非常小的C ++程序调用一些shell命令。
Commands such as "git clone" or "rsync", which require a password. 诸如“ git clone”或“ rsync”之类的命令需要密码。 For example, since git uses SSH, which is interactive, I cannot supply the password to it. 例如,由于git使用交互式的SSH,因此我无法为其提供密码。
My program so far is the following: 到目前为止,我的程序如下:
#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;
}
Output: 输出:
ssh_askpass: exec(/usr/libexec/ssh-askpass): No such file or directory ssh_askpass:exec(/ usr / libexec / ssh-askpass):没有这样的文件或目录
ssh_askpass: exec(/usr/libexec/ssh-askpass): No such file or directory ssh_askpass:exec(/ usr / libexec / ssh-askpass):没有这样的文件或目录
ssh_askpass: exec(/usr/libexec/ssh-askpass): No such file or directory ssh_askpass:exec(/ usr / libexec / ssh-askpass):没有这样的文件或目录
Is there a way for the process to prompt for password just like it would if I would execute the command straight from the command prompt? 是否有一种方法可以像我直接从命令提示符处执行命令一样,提示输入密码?
Thank you! 谢谢!
EDIT: Ideally I would either do it in Python or Shell directly, but my program needs to read different structures in C++ (python bindings would kinda be overkill) so hence why I'm trying to do it in C++. 编辑:理想情况下,我将直接在Python或Shell中执行此操作,但是我的程序需要读取C ++中的不同结构(python绑定有点过大),因此,为什么我要在C ++中执行此操作。
You have two choices. 你有两个选择。
First, for those that have one, you can use each program's method for working around this. 首先,对于拥有一个的用户,可以使用每个程序的方法来解决此问题。 For example, git
has GIT_ASKPASS
, and ssh
has key-based authentication. 例如, git
具有GIT_ASKPASS
,而ssh
具有基于密钥的身份验证。
Second, you can use a pty
to communicate with these programs. 其次,您可以使用pty
与这些程序进行通信。 You can do this with a program such as expect
, or with your own code (see the posix_openpt
command). 您可以使用一个程序,如做到这一点expect
,或用自己的代码(见posix_openpt
命令)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.