加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

使用popen和专用的TTY Python运行交互式Bash

发布时间:2020-12-13 19:19:05 所属栏目:Linux 来源:网络整理
导读:我需要在 Python中使用它自己的专用TTY在一个单独的进程中运行一个交互式Bash实例(我不能使用pexpect). 我使用了这个代码片段,我常见于类似的程序中使用: master,slave = pty.openpty()p = subprocess.Popen(["/bin/bash","-i"],stdin=slave,stdout=slave,s
我需要在 Python中使用它自己的专用TTY在一个单独的进程中运行一个交互式Bash实例(我不能使用pexpect).
我使用了这个代码片段,我常见于类似的程序中使用:
master,slave = pty.openpty()

p = subprocess.Popen(["/bin/bash","-i"],stdin=slave,stdout=slave,stderr=slave)

os.close(slave)

x = os.read(master,1026)

print x

subprocess.Popen.kill(p)
os.close(master)

但是当我运行它时,我得到以下输出:

$./pty_try.py
bash: cannot set terminal process group (10790): Inappropriate ioctl for device
bash: no job control in this shell

运行的Strace显示一些错误:

...
readlink("/usr/bin/python2.7",0x7ffc8db02510,4096) = -1 EINVAL (Invalid argument)
...
ioctl(3,SNDCTL_TMR_TIMEBASE or SNDRV_TIMER_IOCTL_NEXT_DEVICE or TCGETS,0x7ffc8db03590) = -1 ENOTTY (Inappropriate ioctl for device)
...
readlink("./pty_try.py",0x7ffc8db00610,4096) = -1 EINVAL (Invalid argument)

代码片段看起来很简单,Bash没有得到它需要的东西吗?这可能是什么问题?

解决方法

这是在子进程中运行交互式命令的解决方案.它使用伪终端使stdout非阻塞(也有一些命令需要tty设备,例如bash).它使用select来处理输入和输出到子进程.
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
import sys
import select
import termios
import tty
import pty
from subprocess import Popen

command = 'bash'
# command = 'docker run -it --rm centos /bin/bash'.split()

# save original tty setting then set it to raw mode
old_tty = termios.tcgetattr(sys.stdin)
tty.setraw(sys.stdin.fileno())

# open pseudo-terminal to interact with subprocess
master_fd,slave_fd = pty.openpty()

# use os.setsid() make it run in a new process group,or bash job control will not be enabled
p = Popen(command,preexec_fn=os.setsid,stdin=slave_fd,stdout=slave_fd,stderr=slave_fd,universal_newlines=True)

while p.poll() is None:
    r,w,e = select.select([sys.stdin,master_fd],[],[])
    if sys.stdin in r:
        d = os.read(sys.stdin.fileno(),10240)
        os.write(master_fd,d)
    elif master_fd in r:
        o = os.read(master_fd,10240)
        if o:
            os.write(sys.stdout.fileno(),o)

# restore tty settings back
termios.tcsetattr(sys.stdin,termios.TCSADRAIN,old_tty)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读