4

I'm trying to execute a bash command from c and retrieve and show the result. I've tried with system but it doesn't work. My code looks like:

char command[200];
sprintf(command,"lsof -iTCP:%d | cut -d\"\" -f1 | tail -1",port);
printf("Port %d is open\n and is listened by %s",port,system(command));

Please help. I need this .

3
  • consider re-phrasing the question so that it is clear that you are not in fact trying to start a bash command, but in reality want to find processes listening on a specific port (from c++) Commented Nov 13, 2011 at 14:54
  • I'm trying to run a bash command. not to find the service that is listening a port Commented Nov 14, 2011 at 19:29
  • Ok, thanks for clearing that that up Commented Nov 14, 2011 at 19:39

2 Answers 2

5

Edit aside from the actual question, I'd be using

sudo netstat -tlpn

(shows the processes that are listening on TCP ports, not resolving the ports/addresses)

Perhaps combine it with a bit of grep:

sudo netstat -tlpn | grep :7761

to find where port :7761 is being listened?


You can use popen.

With popen you get the benefit that you receive the process output asynchronously (you will be able to stop processing if the answer is on the first line of output without having to wait for the subprocess to complete; simply pclose and the subprocess will die with SIGPIPE)

A sample straight from the Standards Documentation:

The following example demonstrates the use of popen() and pclose() to execute the command ls * in order to obtain a list of files in the current directory:

#include <stdio.h>
...


FILE *fp;
int status;
char path[PATH_MAX];


fp = popen("ls *", "r");
if (fp == NULL)
    /* Handle error */;


while (fgets(path, PATH_MAX, fp) != NULL)
    printf("%s", path);


status = pclose(fp);
if (status == -1) {
    /* Error reported by pclose() */
    ...
} else {
    /* Use macros described under wait() to inspect `status' in order
       to determine success/failure of command executed by popen() */
    ...
}
Sign up to request clarification or add additional context in comments.

6 Comments

it is fast ? i scan lots of ports
@Baden Sorin - you're starting a bash shell and 3 other programs, along with all the associated plumbing, all of which has to be brought into memory, run, then broken down again at the end. No, it's not 'fast'. Depending on what you are doing, it may be 'fast enough', but if it's not, the problem isn't in the 'getting data back from the shell' portion, it's in the 'running a shell command' portion. If you need faster, then you need to find a way to do this without shelling out.
@Michael Kohne the problem is that i need to retrieve the service that is listening a specific port.
@Badea Sorin - that's fine, and it's a workable way to do it. But you asked if it was fast, and I wanted to make clear that shelling out is by definition slow. If you need speed, you're going to have to figure out how to do what lsof is doing on your own, or find a way to get it to spit out a lot of data in one go or something. Shelling out will NEVER be fast.
@BadeaSorin: huh - that is completely not your question. However, edited my answer with netstat usage that should help you. (some UNIX-en won't support this)
|
1

system(command) returns the return code of the command, not its output. If you want to read the output of a command, you should use popen This returns a file descriptor to the output, which you can read from just like a normal file.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.