如何在C中向进程发送输入?
我正在尝试从C运行git push:如何在C中向进程发送输入?,c,io,C,Io,我正在尝试从C运行git push: system("git push"); 当它要求 username: password: 我想还给它一个用户名和一个github身份验证令牌。我将如何做到这一点?我试着四处寻找解决方案,但当我用谷歌搜索这个问题时,我似乎找不到正确的措辞。请注意,用户名和身份验证令牌存储在char*中: char *username = "whatever"; char *token = "whatever"; system("git push"); 例如: 以下示例说
system("git push");
当它要求
username:
password:
我想还给它一个用户名和一个github身份验证令牌。我将如何做到这一点?我试着四处寻找解决方案,但当我用谷歌搜索这个问题时,我似乎找不到正确的措辞。请注意,用户名和身份验证令牌存储在char*
中:
char *username = "whatever";
char *token = "whatever";
system("git push");
例如:
以下示例说明了如何使用execv执行ls shell命令:
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
main()
{
pid_t pid;
char *const parmList[] = {"/bin/ls", "-l", "/u/userid/dirname", NULL};
if ((pid = fork()) == -1)
perror("fork error");
else if (pid == 0) {
execv("/bin/ls", parmList);
printf("Return not expected. Must be an execv error.n");
}
}
#包括
#包括
#包括
main()
{
pid_t pid;
char*const parmList[]={“/bin/ls”、“-l”、“/u/userid/dirname”,NULL};
如果((pid=fork())=-1)
perror(“分叉错误”);
否则如果(pid==0){
execv(“/bin/ls”,parmList);
printf(“不应返回。必须是execv错误。n”);
}
}
您可以根据您的代码进行更改
例如:
以下示例说明了如何使用execv执行ls shell命令:
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
main()
{
pid_t pid;
char *const parmList[] = {"/bin/ls", "-l", "/u/userid/dirname", NULL};
if ((pid = fork()) == -1)
perror("fork error");
else if (pid == 0) {
execv("/bin/ls", parmList);
printf("Return not expected. Must be an execv error.n");
}
}
#包括
#包括
#包括
main()
{
pid_t pid;
char*const parmList[]={“/bin/ls”、“-l”、“/u/userid/dirname”,NULL};
如果((pid=fork())=-1)
perror(“分叉错误”);
否则如果(pid==0){
execv(“/bin/ls”,parmList);
printf(“不应返回。必须是execv错误。n”);
}
}
您可以根据您的代码进行更改如果输入数据是使用
系统(char*命令)创建的,则无法将其发送到流程
(或者可能,但我认为太难了),您需要使用popen(constchar*命令,constchar*类型)创建一个新进程代码>。(). 这是我写的一篇小文章:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
char username[] = "something";
char password[] = "something";
FILE *pf; // file descriptor of the git process
pf = popen("git push", "w"); // create git process in write mode
if (!pf) {
printf("Process couldn't be created!");
return 1;
}
// this will send username and password (and the '\n' char too) to the git process
fputs(username, pf);
fputs(password, pf);
pclose(pf); // close git process
return 0;
}
#包括
#包括
int main(int argc,字符**argv)
{
字符用户名[]=“某物”;
字符密码[]=“某物”;
FILE*pf;//git进程的文件描述符
pf=popen(“git push”,“w”);//在写模式下创建git进程
如果(!pf){
printf(“无法创建进程!”);
返回1;
}
//这将向git进程发送用户名和密码(以及“\n”字符)
fputs(用户名,pf);
fputs(密码,pf);
pclose(pf);//关闭git进程
返回0;
}
如果输入数据是使用系统(char*命令)创建的,则无法将其发送到进程
(或者可能,但我认为太难了),您需要使用popen(constchar*命令,constchar*类型)创建一个新进程代码>。(). 这是我写的一篇小文章:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
char username[] = "something";
char password[] = "something";
FILE *pf; // file descriptor of the git process
pf = popen("git push", "w"); // create git process in write mode
if (!pf) {
printf("Process couldn't be created!");
return 1;
}
// this will send username and password (and the '\n' char too) to the git process
fputs(username, pf);
fputs(password, pf);
pclose(pf); // close git process
return 0;
}
#包括
#包括
int main(int argc,字符**argv)
{
字符用户名[]=“某物”;
字符密码[]=“某物”;
FILE*pf;//git进程的文件描述符
pf=popen(“git push”,“w”);//在写模式下创建git进程
如果(!pf){
printf(“无法创建进程!”);
返回1;
}
//这将向git进程发送用户名和密码(以及“\n”字符)
fputs(用户名,pf);
fputs(密码,pf);
pclose(pf);//关闭git进程
返回0;
}
这看起来像是命令的参数,而不是命令的输入?这看起来像是命令的参数,而不是命令的输入?呃,我认为auth令牌不允许你推送,是吗?考虑使用私钥,是的,我已经测试过了。参考资料上说你可以:)可能有更好的方法通过git存储库来验证你自己。你能给我举个例子吗?呃,我认为auth令牌不允许你推送,是吗?考虑使用私钥,是的,我已经测试过了。参考资料说你可以:)也许有更好的方法可以通过git存储库验证你自己。你能给我举一些例子吗?