如何在点击键盘中断子进程时将最后一个输出保存在变量中

2024-03-29 08:17:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我对子流程模块完全陌生。我试图自动执行反身份验证攻击命令。当我运行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

我可以试着解决这个问题吗


1条回答
网友
1楼 · 发布于 2024-03-29 08:17:53

只需使用Python的error handling。使用tryexcept语句捕获任何KeyboardInnterrupt(在子流程函数中),如下所示:

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

相关问题 更多 >