问题描述
我有一个使用 STDIN 3
(/proc/xxxx/fd/0->/dev/pts/3
)的终端
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
该命令显示在我的第一个( pts/3
)终端中,但未执行该命令.如果(在这个终端 pts/3
中)我在一个程序中等待来自 stdin
的一些数据,数据会写在屏幕上,但程序没有捕获它来自 stdin
.
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
.
我想做的是执行命令"do_something_command"
,不仅显示它.
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?
推荐答案
我完全理解您的要求.您可以通过自己用C编写和执行一小段代码来实现这一点.这应该给您一些想法.
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);
}
使用 sudo
权限编译并执行.例如,如果要在/dev/pts/3
上执行命令,则只需执行 sudo ./a.out -n/dev/pts/3 whoami
,在/dev/pts/3
上运行 whoami
.
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
.
此代码完全取自此页面.
这篇关于通过/dev/pts 在另一个终端中执行命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!