Execute a command in another terminal via /dev/pts

半腔热情 提交于 2019-12-08 17:13:59

问题


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?


回答1:


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.




回答2:


You seem to use the wrong quotes around the command. Either remove the quotes and the echo command, or use echo and back-ticks (`).

Try:

echo `date` > /dev/pts/3

or just

date > /dev/pts/3

Note that whatever runs on /dev/pts/3 wouldn't be able to read what pops up "from behind".



来源:https://stackoverflow.com/questions/24725051/execute-a-command-in-another-terminal-via-dev-pts

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!