Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute a command in another terminal via /dev/pts

Tags:

linux

stdin

pts

I have a terminal that uses STDIN 3 (/proc/xxxx/fd/0 -> /dev/pts/3)

So if (in another terminal) I do:

echo 'do_something_command' > /dev/pts/3

The command is shown in my first (pts/3) terminal, but the command is not executed. And if (in this terminal pts/3) I'm in a program waiting for some data from stdin, the data is written on screen but the program does not capture it from stdin.

What I want to do is execute the command "do_something_command" and not only show it.

Can someone explain this behavior to me? How do I achieve my intention?

like image 772
ton Avatar asked Jul 13 '14 16:07

ton


People also ask

How do you execute a Python program from terminal?

But really large Python programs with a lot of complexity are written in files with a .py extension, typically called Python scripts. Then you execute them from the terminal using the Python command. All the commands we executed previously via the shell, we can also write it in a script and run in this way.

What is the /dev/pts directory in Linux?

The /dev/pts is a special virtual temporary file directory unique to the Linux system while running the programs through pseudoterminals.

What is a terminal and how does it work?

What is a Terminal? The terminal is the program that interacts with the shell and allows us to communicate with it via text-based commands. This is why it's also called the command line. To access the terminal on Windows, hit the Windows logo + R, type cmd, and press Enter.

What is the meaning of /dev/pts?

The /dev/pts signifies the master files related to the pseudoterminal slave or a master, and the system saves it as /dev/ptmx. You can open the descriptors of these master files using the grantpt and unlockpt commands.


1 Answers

I completely get what you are asking. You can achieve this by writing and executing a small piece of code in C yourself. This should give you some idea.

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

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

Compile and execute it with sudo permissions. For example, if you want to execute a command on /dev/pts/3, then simply do a sudo ./a.out -n /dev/pts/3 whoami, runs a whoami on /dev/pts/3.

This code was completely taken from this page.

like image 200
John Strood Avatar answered Sep 26 '22 00:09

John Strood