在嵌入式终端发出指令 [英] Giving a command in a embedded terminal

查看:206
本文介绍了在嵌入式终端发出指令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用下面的python code嵌入在Tkinter的窗口终端窗口(从Ubuntu Linux操作系统)。我想发出命令窗口中的SH kBegin'终端窗口启动时自动:

I'm using the following python code to embed a terminal window (from Ubuntu Linux) in a Tkinter window. I would like give the command 'sh kBegin' in the window automatically when the terminal window starts:

from Tkinter import *
from os import system as cmd

root = Tk()
termf = Frame(root, height=800, width=1000)

termf.pack(fill=BOTH, expand=YES)
wid = termf.winfo_id()
cmd('xterm -into %d -geometry 160x50 -sb &' % wid)

root.mainloop()

伪:

cmd('xterm -into %d -geometry 160x50 -sb &' % wid)
embedded_terminal('sh kBegin')
# EMBEDDED TERMINAL DISPLAYS OUTPUT OF sh kBegin##

我将如何得到这个工作?

How would I get this working?

推荐答案

您可以用外壳由伪终端从孩子写互动。这里的作品怎么会演示。这个答案是有点基于一个答案的Linux伪终端:执行从一个终端发送串另一个

You can interact with a shell by writing in the pseudo-terminal slave child. Here is a demo of how it could works. This answer is somewhat based on an answer to Linux pseudo-terminals: executing string sent from one terminal in another.

问题的关键是让伪终端通过使用的xterm(通过的tty 命令)和重定向输出和你的方法输入这个伪终端文件。例如 LS<为/ dev / PTS / 1>为/ dev / PTS / 1 2 - ;为/ dev / PTS / 1

The point is to get the pseudo-terminal used by xterm (through tty command) and redirect output and input of your method to this pseudo-terminal file. For instance ls < /dev/pts/1 > /dev/pts/1 2> /dev/pts/1

注意


  1. 处理xterm的孩子被泄露(使用使用os.system 不建议,尤其是对&安培; 的说明见 suprocess 模块)。

  2. 它可能无法以编程找到使用哪个TTY

  3. 每个命令在一个新的suprocess(仅输入和输出显示),因此状态修改命令,如 CD 执行没有任何效果,以及上下文xterm中( CD 中的xterm)

  1. xterm child processed are leaked (the use of os.system is not recommended, especially for & instructions. See suprocess module).
  2. it may not be possible to find programmatically which tty is used
  3. each commands are executed in a new suprocess (only input and output is displayed), so state modification command such as cd have no effect, as well as context of the xterm (cd in the xterm)

from Tkinter import *
from os import system as cmd

root = Tk()
termf = Frame(root, height=700, width=1000)
termf.pack(fill=BOTH, expand=YES)
wid = termf.winfo_id()

f=Frame(root)
Label(f,text="/dev/pts/").pack(side=LEFT)
tty_index = Entry(f, width=3)
tty_index.insert(0, "1")
tty_index.pack(side=LEFT)
Label(f,text="Command:").pack(side=LEFT)
e = Entry(f)
e.insert(0, "ls -l")
e.pack(side=LEFT,fill=X,expand=1)

def send_entry_to_terminal(*args):
    """*args needed since callback may be called from no arg (button)
   or one arg (entry)
   """
    command=e.get()
    tty="/dev/pts/%s" % tty_index.get()
    cmd("%s <%s >%s 2> %s" % (command,tty,tty,tty))

e.bind("<Return>",send_entry_to_terminal)
b = Button(f,text="Send", command=send_entry_to_terminal)
b.pack(side=LEFT)
f.pack(fill=X, expand=1)

cmd('xterm -into %d -geometry 160x50 -sb -e "tty; sh" &' % wid)

root.mainloop()

这篇关于在嵌入式终端发出指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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