Control Remote Machine using C programs

Here are two programs by which we can control a machine remotely

Last time we learnt how to create a chat programs using the sockets. By which we can communicate on two different machines on a network.

Today, We will do an interesting stuff by creating a pair of programs a remote and a control to control a machine remotely using the sockets through UDP.

In the project we will create two programs 1) remote.c & 2) control.c

remote.c will run on a machine as a server and keep listening for the intructions to be sent by the controller from a remote machine. remote.c program will execute the command sent by the controller, generate the output and sent back the output to the controller.

control.c program will provide an interface to send commands to the remote machine on a network. It will collect command from user and send them to the remote machine and then collect the output from the remote machine and then display to the user.

You can change the IP address and specify the port numbers according to the machine where u want to run remote and the control.

Code for remote.c

/*
 A remote control program using UDP and sockets in linux through system programming
 by Sandeep Nandal 2012
*/


/* It is the remote program which keep running on remote machine and wait for the
 the command sent by the control program from a different machine. It will execute 
execute the command and collect the output and send the output the control machine
 through sockets */

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

int main(int argc, char **argv){
	int x, z;
	char *host_ip = "127.0.0.1"; /*Here we have used localhost address for host machine */
	short host_port = 1234; 	/* It's the port of host machine */
	char *remote_ip = "127.0.0.1"; /* Ip address of remote machine*/
	short remote_port = 1235; 	/* port of remote machine */
	int host_sock, remote_sock;
	char datagram[512];
	char path[1024]={0};
	int remote_len;

	FILE *fp;  /* it will open a stream for the output of command */

	struct sockaddr_in host_add;
	struct sockaddr_in remote_add;


	host_sock = socket(AF_INET, SOCK_DGRAM, 0);
	
	host_add.sin_family = AF_INET;
	host_add.sin_port = htons(host_port);
	host_add.sin_addr.s_addr = inet_addr(host_ip);

	z = bind(host_sock, (struct sockaddr *)&host_add, sizeof(host_add));

	while(1){
		puts("Waiting for command.........");
		bzero(datagram, 512);

		/* Here we are waiting for recieving command from remote machine */
		z = recvfrom(host_sock, datagram, 512, 0, (struct sockaddr *)&remote_add, &remote_len);
		datagram[z] = 0;
		printf("Command:%s", datagram);

		/* opening a stream by executing the command */
		fp = popen(datagram, "r");
		bzero(datagram, sizeof(datagram));

		remote_sock = socket(AF_INET, SOCK_DGRAM, 0);
		remote_add.sin_family = AF_INET;
		remote_add.sin_port = htons(remote_port);
		remote_add.sin_addr.s_addr = inet_addr(remote_ip);

		/* collecting the output n sending to the remote */
		while(fgets(datagram, sizeof(datagram)-1, fp) != NULL){
			x = sendto(remote_sock, datagram, strlen(datagram), 0, (struct sockaddr *)&remote_add, sizeof(remote_add));
			bzero(datagram, sizeof(datagram));
		}
		/* marking the end of output of command */
		strcpy(datagram, "___EOF___");
		x = sendto(remote_sock, datagram, strlen(datagram), 0, (struct sockaddr *)&remote_add, sizeof(remote_add));
		close(remote_sock);
	}
	close(host_sock);
	return 0;
}

compile the remote.c program by gcc through the command

$gcc remote.c -o remote

Code for control.c

/*
 A remote control program using UDP and sockets in linux through system programming
 by Sandeep Nandal 2012
*/

/* By this program a remote program can be made run on the remote machine. and can be controlled by the control program. Here is the code of the control.c program. Which will send the commands to the remote from a different machine and collect the output from remote and display on the control end. */

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

int main(int argc, char **argv){
	int x, z;
	char *host_ip = "127.0.0.1";
	short host_port = 1235;
	char *remote_ip = "127.0.0.1";
	short remote_port = 1234;

	int host_sock, remote_sock;
	char datagram[512];
	int remote_len;

	struct sockaddr_in host_add;
	struct sockaddr_in remote_add;

	host_sock = socket(AF_INET, SOCK_DGRAM, 0);
	
	host_add.sin_family = AF_INET;
	host_add.sin_port = htons(host_port);
	host_add.sin_addr.s_addr = inet_addr(host_ip);

	z = bind(host_sock, (struct sockaddr *)&host_add, sizeof(host_add));

	while(1){		
		printf("\nCommand: ");
		bzero(datagram, 512); 
		fgets(datagram, 512, stdin);

		remote_sock = socket(AF_INET, SOCK_DGRAM, 0);
		remote_add.sin_family = AF_INET;
		remote_add.sin_port = htons(remote_port);
		remote_add.sin_addr.s_addr = inet_addr(remote_ip);

		/* Here the command is sent to remote machine through UDP */
		x = sendto(remote_sock, datagram, strlen(datagram), 0, (struct sockaddr *)&remote_add, sizeof(remote_add));
		if(x != -1){
			puts("Command sent. Waiting for response.........");
		}
		close(remote_sock);
		bzero(datagram, 512);

		/* Here the output of the command is collected from the remote */
		do{
			bzero(datagram, sizeof(datagram));
			z = recvfrom(host_sock, datagram, 512, 0, (struct sockaddr *)&remote_add, &remote_len);
			datagram[z] = 0;
			printf("%s", datagram);
		}while(strncmp(datagram, "___EOF___", 9) != 0);

	}
	close(host_sock);
	return 0;
}

compile the control.c program by gcc through the command

$gcc control.c -o control

Now you are ready with two executables. One you will run on a remote machine as a server. and the other to control that server on the network.

If in doubt, feel free to post your queries.

Twit this with your twitter acount :

2 thoughts on “Control Remote Machine using C programs

  1. Pingback: harvey

  2. Pingback: Jay

Leave a Reply