简体   繁体   English

无法执行 netcat

[英]Can't execlp netcat

I'm trying to exec the following command from C:我正在尝试从 C 执行以下命令:

netcat 127.0.0.1 4444

This is my code:这是我的代码:

#include <unistd.h>

int main() {
    execlp("/usr/bin/netcat", "127.0.0.1", "4444", 0);
}

However, I keep getting the error: Error: no ports specified for connection .但是,我不断收到错误消息: Error: no ports specified for connection

To answer a few questions I feel may come up:回答几个我觉得可能会出现的问题:

  • which netcat gives /usr/bin/netcat which netcat/usr/bin/netcat
  • I've tried using execvp and got the same results我试过使用execvp并得到相同的结果
  • Running the command normally, ie via a terminal, is successful (Usually i also give the -e /bin/bash argument but I've omitted it for simplicity. Adding it doesn't change the results.)正常运行命令,即通过终端,是成功的(通常我也给出-e /bin/bash参数,但为了简单起见,我省略了它。添加它不会改变结果。)

The problem is, that your first argument is "4444".问题是,您的第一个参数是“4444”。 Why?为什么?

Look at this line:看看这一行:

execlp("/usr/bin/netcat", "127.0.0.1", "4444", 0);

The first argument to execlp() is the binary to execute. execlp()第一个参数是要执行的二进制文件。 The rest of the arguments will form the argv[] -vector passed to the process.其余的参数将形成传递给进程的argv[] -vector。

By convention, argv[0] should contain the name of the executable and the first "real" argument is argv[1] , which is "4444" in your case.按照惯例, argv[0]应包含可执行文件的名称,第一个“真实”参数是argv[1] ,在您的情况下为“4444”。 So what you do is the equivalent on the shell of所以你所做的是在 shell 上的等价物

netcat 4444网猫 4444

and the correct call would be:正确的调用是:

execlp("/usr/bin/netcat", "netcat", "127.0.0.1", "4444", 0);

Btw.顺便提一句。 the use of execlp() makes only sense when calling a binary without a full path, since it looks for the correct path itself (using the PATH environment variable). execlp()的使用仅在调用没有完整路径的二进制文件时才有意义,因为它本身会寻找正确的路径(使用 PATH 环境变量)。 Otherwise, execl() should be used.否则,应使用execl() But it will work anyway.但无论如何它都会起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM