exectty

Submitted scripts and programs
Forum rules
Your own work only.
User avatar
elixir
Weight Watcher
Posts: 357
Joined: Fri Feb 21, 2014 8:25 am

exectty

Unread post by elixir » Sat Aug 08, 2015 9:31 am

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;
}

Out of the corner of your eye you spot him... Shia LaBeouf.

https://www.youtube.com/watch?v=o0u4M6vppCI

User avatar
GekkoP
Emacs Sancho Panza
Posts: 5877
Joined: Tue Sep 03, 2013 7:05 am

Re: exectty

Unread post by GekkoP » Sat Aug 08, 2015 9:45 am

Interesting program, thanks for sharing.

User avatar
rhowaldt
Dog
Posts: 4565
Joined: Wed Oct 17, 2012 9:01 am
Contact:

Re: exectty

Unread post by rhowaldt » Sat Aug 08, 2015 4:17 pm

thanks elixir, i am sure somebody will find this useful!
All statements are true in some sense, false in some sense, meaningless in some sense, true and false in some sense, true and meaningless in some sense, false and meaningless in some sense, and true and false and meaningless in some sense.

Post Reply