linux-将命令从一个终端写入另一终端 [英] linux - write commands from one terminal to another

查看:309
本文介绍了linux-将命令从一个终端写入另一终端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从一个终端向另一终端写命令.

I need to write commands from one terminal to another terminal.

我尝试了这些:

echo -e "ls\n" > /proc/pid/fd/0
echo -e "ls\n" > /dev/pts/4

仅将ls打印为输出,并且不执行.

Which just prints the ls as output and doesn't execute.

我尝试了这些:

chmod 777 /dev/tty4 ;echo "ls" > /dev/tty4
chmod 777 /dev/tty40 ;echo "ls" > /dev/tty40

似乎什么也没做

有什么想法吗?

[请注意,我不想触摸第二个终端来完成此操作.只有第一个]

[note that I don't want to touch the second terminal to accomplish this. only the first one]

推荐答案

Python代码:

#!/usr/bin/python

import sys,os,fcntl,termios
if len(sys.argv) != 3:
   sys.stderr.write("usage: ttyexec.py tty command\n")
   sys.exit(1)
fd = os.open("/dev/" + sys.argv[1], os.O_RDWR)
cmd=sys.argv[2]
for i in range(len(cmd)):
   fcntl.ioctl(fd, termios.TIOCSTI, cmd[i])
fcntl.ioctl(fd, termios.TIOCSTI, '\n')
os.close(fd)

这篇关于linux-将命令从一个终端写入另一终端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆