【发布时间】:2019-02-08 22:06:18
【问题描述】:
我正在使用 Python(OSX Python 2.7.10)通过 ssh 进入 Debian 机器(Python 2.7.9),然后运行 bash 脚本(./capture)。 bash 脚本包含一些 tcpdump 命令。我无法弄清楚的问题是如何在终端上显示正在运行的 bash 脚本的实时结果。
#!/usr/bin/env python3
import subprocess, os
output = subprocess.run(["ssh", "ju@192.168.199.125", "sudo ./capture"])
print(output)
我能够 ssh 并成功运行脚本,但我没有得到任何输出。当我按 CTRL C 时,我得到以下跟踪:
**^CTraceback (most recent call last):
File "/Users/junesjoseph/run.py", line 3, in <module>
output = subprocess.run(["ssh", "junes@192.168.199.125", "sudo ./capture"])
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/subprocess.py", line 405, in run
stdout, stderr = process.communicate(input, timeout=timeout)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/subprocess.py", line 835, in communicate
self.wait()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/subprocess.py", line 1457, in wait
(pid, sts) = self._try_wait(0)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/subprocess.py", line 1404, in _try_wait
(pid, sts) = os.waitpid(self.pid, wait_flags)
KeyboardInterrupt**
非常感谢任何帮助。谢谢
【问题讨论】:
标签: python bash ssh subprocess