Created
July 8, 2014 20:22
-
-
Save lidio601/f1eb75374602fbf433e3 to your computer and use it in GitHub Desktop.
Linux: send command from another terminal to a TTY terminal
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# see http://askubuntu.com/questions/194293/how-to-send-terminal-command-to-a-tty-terminal | |
nano ttyecho.c | |
make ttyecho | |
sudo chown root:root ttyecho | |
sudo chmod u+s ttyecho | |
tty | |
> /dev/pts/5 | |
sudo ttyecho -n /dev/pts/5 "ls -lah" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <fcntl.h> | |
#include <sys/stat.h> | |
#include <sys/ioctl.h> | |
#include <string.h> | |
#include <unistd.h> | |
void print_help(char *prog_name) { | |
printf("Usage: %s [-n] DEVNAME COMMAND\n", prog_name); | |
printf("Usage: '-n' is an optional argument if you want to push a new line at the end of the text\n"); | |
printf("Usage: Will require 'sudo' to run if the executable is not setuid root\n"); | |
exit(1); | |
} | |
/** | |
@see http://askubuntu.com/questions/194293/how-to-send-terminal-command-to-a-tty-terminal | |
*/ | |
int main (int argc, char *argv[]) { | |
char *cmd, *nl = "\n"; | |
int i, fd; | |
int devno, commandno, newline; | |
int mem_len; | |
devno = 1; commandno = 2; newline = 0; | |
if (argc < 3) { | |
print_help(argv[0]); | |
} | |
if (argc > 3 && argv[1][0] == '-' && argv[1][1] == 'n') { | |
devno = 2; commandno = 3; newline=1; | |
} else if (argc > 3 && argv[1][0] == '-' && argv[1][1] != 'n') { | |
printf("Invalid Option\n"); | |
print_help(argv[0]); | |
} | |
fd = open(argv[devno],O_RDWR); | |
if(fd == -1) { | |
perror("open DEVICE"); | |
exit(1); | |
} | |
mem_len = 0; | |
for ( i = commandno; i < argc; i++ ) { | |
mem_len += strlen(argv[i]) + 2; | |
if ( i > commandno ) { | |
cmd = (char *)realloc((void *)cmd, mem_len); | |
} else { //i == commandno | |
cmd = (char *)malloc(mem_len); | |
} | |
strcat(cmd, argv[i]); | |
strcat(cmd, " "); | |
} | |
if (newline == 0) | |
usleep(225000); | |
for (i = 0; cmd[i]; i++) | |
ioctl (fd, TIOCSTI, cmd+i); | |
if (newline == 1) | |
ioctl (fd, TIOCSTI, nl); | |
close(fd); | |
free((void *)cmd); | |
exit (0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment