gpt4 book ai didi

c - 如何从客户端向服务器发送unix命令然后返回结果?

转载 作者:太空宇宙 更新时间:2023-11-04 04:05:26 24 4
gpt4 key购买 nike

我正在尝试从客户端向服务器发送 unix 命令,等待服务器执行它然后将结果返回给客户端。

我设法让连接正常工作,但我不知道如何继续。这是我应该走的方向吗?

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>

#define MAXLINE 1024

int main(void)
{
int pfd[2], n
pid_t pid;

char buf[MAXLINE]
char test[] = "pipe test\n";

if (pipe(pfd) < 0)
perror("pipe error");

if ((pid = fork()) < 0)
perror("fork error");
else
if (pid == 0)
{
close(pfd[1]);
n = read(pfd[0], buf, MAXLINE);
printf("read %d byte: \n", n);
fflush(stdout);
write(1, buf, n);
}
else
{
close(pfd[0]);
write(pfd[1], test, sizeof(test));
}
exit(0);
}

感谢帮助,ty坂东

最佳答案

你想要 popen(3) 但请注意基于不受信任的用户输入在服务器上运行命令的安全隐患 - 这是一个很大的禁忌。

关于c - 如何从客户端向服务器发送unix命令然后返回结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6608142/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com