简体   繁体   English

带有subprocess.Popen的python osx终端线程

[英]python osx terminal thread with subprocess.Popen

I have a program in Python that uses subprocess and Popen, however when I run a particular binary, an iOS tool called itnl/itunnel, it keeps the terminal window open for logging purposes and therefore I cannot write any further commands to that terminal such as the ssh command. 我有一个使用子进程和Popen的Python程序,但是,当我运行一个特定的二进制文件时,这是一个称为itnl / itunnel的iOS工具,它将终端窗口保持打开状态以进行记录,因此我无法向该终端写入任何其他命令,例如ssh命令。

I have played around with various options including creationflags that I found in another SO thread but it didn't seem to work on OSX. 我玩过各种选项,包括在另一个SO线程中找到的creationflags,但它似乎在OSX上不起作用。

Running OSX and python 2.7 on homebrew install. 在自制安装上运行OSX和python 2.7。

if ("iPhone" in stdout or "iPad" in stdout):
        self.progressBox.AppendText('Device found. Starting iTunnel\n')
        cmd = toolsDir + "/itnl --iport 22 --lport 2222"
        p = subprocess.Popen(cmd, shell=True,stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
        stdout = p.communicate()[0]
        if "Device connected" in stdout:
            cmd = "ssh -p 2222 root@127.0.0.1"
            p = subprocess.Popen(cmd, shell=True,stdout=subprocess.PIPE, stderr=subprocess.STDOUT)

Once itunnel runs it sits there waiting for connections from the device like below: 一旦itunnel运行,它就会坐在那里等待来自设备的连接,如下所示:

 [INFO] Waiting for new TCP connection on port 2222
 [INFO] Waiting for device...
 [INFO] Device connected: 6a0a098xxxxxxxx3cb2ef9ef6205

(this is when you type 'ssh etc etc' to complete the connection) (这是当您键入“ ssh等”以完成连接时)

p.communicate() will block until the process has terminated so it can return all of the output from that process. p.communicate()将阻塞直到该进程终止,以便它可以返回该进程的所有输出。 You want to use it in a non-blocking mode, searching for that here on SO should turn up some good examples. 您想在非阻塞模式下使用它,因此在SO上进行搜索应该可以找到一些很好的例子。

You may also want to take a look at using the pexpect module. 您可能还想看看如何使用pexpect模块。

found a way round my issue by sending any output to dev/null and backgrounding the process: 通过将任何输出发送到dev / null并后台处理该进程,找到了解决我的问题的方法:

./itnl --iport 22 --lport 2222 > /dev/null 2>&1 &

not ideal as I now can't get the stdout to see if a device has connected but it returns me to the terminal prompt and allows further commands. 这并不理想,因为我现在无法获得标准输出来查看设备是否已连接,但是它使我返回到终端提示符并允许其他命令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM