Hi,

In java, we can run a program remotely by doing something like this:

Runtime.exec("ssh machinename programname")

I was wondering whether I can do the same in C using some standard library function call. Also is this possible to get a handle to the remote program's input/output to see its console output or provide input (as done in java)?

Thanks.

Hi,

In java, we can run a program remotely by doing something like this:

Runtime.exec("ssh machinename programname")

I was wondering whether I can do the same in C using some standard library function call. Also is this possible to get a handle to the remote program's input/output to see its console output or provide input (as done in java)?

Thanks.

I'm going to say no because C's standard library doesn't include that kind of functionality....Do operating system's like Windows, Linux and Mac include C libraries that include that functionality? Yes.

For Linux/Unix you would include the library unistd.h (unix standard) to include the family of functions: execl, execlp, execle, execv, execvp

DESCRIPTION
The exec() family of functions replaces the current process image with
a new process image. The functions described in this manual page are
front-ends for execve(2). (See the manual page for execve(2) for fur‐
ther details about the replacement of the current process image.)

I'm going to say no because C's standard library doesn't include that kind of functionality....Do operating system's like Windows, Linux and Mac include C libraries that include that functionality? Yes.

For Linux/Unix you would include the library unistd.h (unix standard) to include the family of functions: execl, execlp, execle, execv, execvp

DESCRIPTION
The exec() family of functions replaces the current process image with
a new process image. The functions described in this manual page are
front-ends for execve(2). (See the manual page for execve(2) for fur‐
ther details about the replacement of the current process image.)

I understand the inputs can be provided using the args parameters but how to get file handle of the output of the created processes? Also does this work for ssh also as it seems to be different than any other cases involving local programs.

I understand the inputs can be provided using the args parameters but how to get file handle of the output of the created processes? Also does this work for ssh also as it seems to be different than any other cases involving local programs.

What your asking is probably operating system specific...For Linux/Unix its a matter of forking the process and then replacing one/both of the forks with your new process

#include <unistd.h>

enum PIPES {READ, WRITE};

int main(int argc, char**argv)
{
	if (argv[2])
	{
		int hpipe[2];
		pipe(hpipe);

		if (fork())
		{
			close (hpipe[WRITE]);
			dup2 (hpipe[READ], 0);
			close (hpipe[READ]);
			execlp (argv[2],argv[2],NULL);
		}
		else
		{
			close (hpipe[READ]);
			dup2 (hpipe[WRITE], 1);
			close (hpipe[WRITE]);
			execlp (argv[1],argv[1],NULL);
		}
	}
	return 0;
}

This is a standard example for Linux. You can't do this in Windows, well at least the fork part..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.