【问题标题】:How to save the last output in variable when hit KeyboardInterrupt subprocess命中KeyboardInterrupt子进程时如何将最后一个输出保存在变量中
【发布时间】:2020-09-23 18:27:51
【问题描述】:

我对子流程模块完全陌生。我试图自动化解除身份验证攻击命令。如您所知,当我运行 airodump-ng wlan0mon 时,它会查找附近的 AP 以及与之连接的客户端。

现在,当我尝试使用 Python 中的 p = subprocess.run(["airmon-ng","wlan0mon"], capture_output=True) 运行此命令时,您知道此命令一直运行到用户按 Ctrl+C,因此当用户在变量中按 Ctrl+C 时,它应该保存最后的输出,但是相反,我得到的错误是:

Traceback (most recent call last):
  File "Deauth.py", line 9, in <module>
    p3 = subprocess.run(["airodump-ng","wlan0"], capture_output=True)
  File "/usr/lib/python3.8/subprocess.py", line 491, in run
    stdout, stderr = process.communicate(input, timeout=timeout)
  File "/usr/lib/python3.8/subprocess.py", line 1024, in communicate
    stdout, stderr = self._communicate(input, endtime, timeout)
  File "/usr/lib/python3.8/subprocess.py", line 1866, in _communicate
    ready = selector.select(timeout)
  File "/usr/lib/python3.8/selectors.py", line 415, in select
    fd_event_list = self._selector.poll(timeout)
KeyboardInterrupt

我可以尝试什么来解决这个问题?

【问题讨论】:

标签: python linux subprocess kali-linux


【解决方案1】:

只需使用 Python 的error handling。使用try 和except 语句捕获任何KeyboardInnterrupts(在您的子进程函数中),如下所示:

def stuff(things):
  try:
    # do stuff
  except KeyboardInterrupt:
    return last_value

【讨论】:

  • 你能解释一下吗,因为我不明白。也许您可以为我在问题中提供的命令编写它。顺便谢谢你的回答。
猜你喜欢
  • 2017-04-21
  • 2021-11-06
  • 2021-10-10
  • 2020-03-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-05
  • 1970-01-01
相关资源
最近更新 更多