You are not logged in.

#1 2012-09-01 04:36:02

tladuke
Member
Registered: 2009-07-23
Posts: 171

dead simplest way to run a program on a remote machine?

Hi guys,
I'm drawing a blank because it's so simple/stupid. I need to fake something for now and then do it the right way later. Say I have two Arch machines with GPIO pins and a driver/library for the pins in c. I have a button on each machine and an LED on each machine. I want to push the button on one and have the light on the other light up and vice versa. I have little c programs that can set and read the pins.  While typing this I thought of netcat, run a server on each. I can run some arbitrary bash from a c program right?

Offline

#2 2012-09-01 16:01:36

cmtptr
Member
Registered: 2008-09-01
Posts: 135

Re: dead simplest way to run a program on a remote machine?

If you already have an ssh server running on each, that's what I would use.

$ ssh user@address /path/to/your/exe --args...

Otherwise, I guess you could use some netcat magic to make a server.  Playing around with gnu-netcat for a few minutes, I haven't been able to figure out how to get the listening netcat to keep listening after the a client disconnects, though.  Another option is to just stick some server code straight into your C program to listen to anonymous requests on a UDP port.  Something along the lines of...

#define _POSIX_SOURCE

#include <netdb.h>
#include <string.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>

#define PORT_LISTEN 12345

#define MACRO_TO_STR_(m) #m
#define MACRO_TO_STR(m) MACRO_TO_STR_(m)

int main(/*int argc, char **argv*/)
{
	struct addrinfo hints;
	struct addrinfo *res;
	int sock;

	memset(&hints, 0, sizeof hints);
	hints.ai_family = AF_UNSPEC;
	hints.ai_socktype = SOCK_DGRAM;
	hints.ai_flags = AI_PASSIVE;

	getaddrinfo(0, MACRO_TO_STR(PORT_LISTEN), &hints, &res);
	sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
	bind(sock, res->ai_addr, res->ai_addrlen);
	freeaddrinfo(res);

	do {
		fd_set sock_set;
		ssize_t len;
		char buffer[128];
		struct sockaddr_storage addr;
		socklen_t addr_len;

		FD_ZERO(&sock_set);
		FD_SET(sock, &sock_set);
		select(sock + 1, &sock_set, 0, 0, 0);

		addr_len = sizeof addr;
		len = recvfrom(sock, buffer, sizeof buffer, 0,
				(struct sockaddr *)&addr, &addr_len);
		if (len > 0)
			/* do your thing here */;
	} while (1);

	close(sock);
	return 0;
}

Run it, and then trigger it from another machine by sending it a blank string...

$ nc -cu address 12345 <<< ""

Offline

#3 2012-09-02 20:31:47

Darksoul71
Member
Registered: 2010-04-12
Posts: 319

Re: dead simplest way to run a program on a remote machine?

Ensure that passwordless login via ssh works from and to each machine. Then do what cmtptr said and call the script at the other machine via ssh.
GPIO ? You are running arch on a Raspi, right ?


My archlinux x86_64 host:
AMD E350 (2x1.6GHz) / 8GB DDR3 RAM / GeForce 9500GT (passive) / Arch running from 16GB USB Stick

Offline

#4 2012-09-04 20:28:16

tladuke
Member
Registered: 2009-07-23
Posts: 171

Re: dead simplest way to run a program on a remote machine?

Darksoul71 wrote:

Ensure that passwordless login via ssh works from and to each machine. Then do what cmtptr said and call the script at the other machine via ssh.
GPIO ? You are running arch on a Raspi, right ?

Something similar. I did just get one in the mail today.

Offline

#5 2012-10-01 22:54:25

tladuke
Member
Registered: 2009-07-23
Posts: 171

Re: dead simplest way to run a program on a remote machine?

I got a working UDP client and server working, but it's a little bit of a mess. Do people make abstraction libraries for this type of thing? I think I want to switch to TCP.
Or I could do the networking part in a scripting language and just rely on c for the GPIO parts, but that's a different mess...

Offline

#6 2012-10-01 23:44:20

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 13,167

Re: dead simplest way to run a program on a remote machine?

It is really dead simple in Python.
http://docs.python.org/library/socketserver.html


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
You assume people are rational and influenced by evidence.  You must not work with the public much. -- Trilby
----
How to Ask Questions the Smart Way

Offline

#7 2012-10-02 05:37:20

Darksoul71
Member
Registered: 2010-04-12
Posts: 319

Re: dead simplest way to run a program on a remote machine?

+1
Networking in Python is dead simple...at least compared to other languages smile


My archlinux x86_64 host:
AMD E350 (2x1.6GHz) / 8GB DDR3 RAM / GeForce 9500GT (passive) / Arch running from 16GB USB Stick

Offline

Board footer

Powered by FluxBB