Page 1 of 1

exectty

Posted: Sat Aug 08, 2015 9:31 am
by elixir
Hey grillers,

I wrote a C program that allows you to execute commands to a specific tty/pts. I believe there are a fair number of possibilities for a program such as this, keeping in mind of a wm switching script I have been working on.

I found the original concept to the code online but made major modifications.

BBQ toolbox worthy?

Code: Select all

zac@bizware:~/Programming/C$ ./exectty 
Usage: exectty /dev/ttyX "command"
Usage: Must run as root

Code: Select all

zac@bizware:~/Programming/C$ sudo ./exectty /dev/tty2 "echo testing"
zac@bizware:~/Programming/C$ sudo ./exectty /dev/tty2 links

Code: Select all

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <string.h>

void usage(char *name) {
	printf("Usage: exectty /dev/ttyX \"command\"\n");
	printf("Usage: Must run as root\n");
	exit(1);
}

int main (int argc, char *argv[]) {
	char *nl = "\n";
	char *cmd;
	int i, device;
	int tty = 1;
	int command = 2;
	int mem = 0;
	
	if (argc < 2) {
		usage(argv[0]);
	}
	
	if (geteuid() != 0) {
		usage(argv[0]);
	}
	
	device = open(argv[tty], O_RDWR);

	if (device == -1) {
		perror("Device is not open.");
		exit(1);
	}

	mem = strlen(argv[command]);
	cmd = malloc(mem);
	
	strcat(cmd, argv[command]);
	
	for (i = 0; cmd[i]; i++) {
		ioctl(device, TIOCSTI, cmd+i);
	}
	
	ioctl(device, TIOCSTI, nl);
	
	close(device);
	free(cmd);
	return 0;
}


Re: exectty

Posted: Sat Aug 08, 2015 9:45 am
by GekkoP
Interesting program, thanks for sharing.

Re: exectty

Posted: Sat Aug 08, 2015 4:17 pm
by rhowaldt
thanks elixir, i am sure somebody will find this useful!