如何向不同的终端执行命令?
用到的c语言代码:run.c
#include <stdio.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
int main(int argc, char **argv)
{
char tty[16] = {0};
char cmd[64] = {0};
char tmp[2] = {0};
int len, i;
int fd;
strncpy(tty, argv[1], strlen(argv[1]));
strncpy(cmd, argv[2], strlen(argv[2]));
len = strlen(cmd);
fd = open(tty, O_RDWR);
for (i = 0; i < len; i++) {
sprintf(tmp, "%c ", cmd[i]);
ioctl(fd, TIOCSTI, tmp);
}
ioctl(fd, TIOCSTI, "\n ");
}
编译c语言代码:
gcc run.c -o a.out
在pts0执行想pts1终端执行命令
[root@pts0 ~]# tty
/dev/pts/0 #这是当前的终端
[root@pts0 ~]# ./a.out /dev/pts/1 "hostname -I" #向pts1终端执行命令
[root@pts0 ~]#
在pts1终端查看执行结果:
[root@pts1 ~]# hostname -I
176.16.0.51
[root@pts1 ~]# tty
/dev/pts/1
[root@pts1 ~]#
#!/bin/bash
#tty_o=\$(tty)
tty_o="/dev/pts/1"
pushd /tmp/bash_test
./a.out \$tty_o "sleep 25s &"
sleep 8
[ "\$1" == "jobs" ] && \
./a.out \$tty_o "jobs > /tmp/bash_test/result"
[ "\$1" == "jobsl" ] && \
./a.out \$tty_o "jobs -l > /tmp/bash_test/result"
[ "\$1" == "jobsn" ] && \
./a.out \$tty_o "jobs -n > /tmp/bash_test/result"
[ "\$1" == "jobsp" ] && \
./a.out \$tty_o "jobs -p > /tmp/bash_test/result"
[ "\$1" == "jobsr" ] && \
./a.out \$tty_o "jobs -r > /tmp/bash_test/result"
[ "\$1" == "jobss" ] && {
./a.out \$tty_o "killall -STOP sleep"
./a.out \$tty_o "jobs -s > /tmp/bash_test/result"
}
popd