[英]When to use subprocess.call() or subprocess.Popen(), running airodump
I have this little script that puts your wireless device into monitor mode. 我有这个小脚本,可以让您的无线设备进入监控模式。 It does an airodump scan and then after terminating the scan dumps the output to file.txt or a variable, so then I can scrape the BSSID and whatever other info I may need.
它执行airodump扫描,然后在终止扫描后将输出转储到file.txt或变量,这样我就可以刮掉BSSID以及我可能需要的任何其他信息。
I feel I haven't grasped the concept or difference between subprocess.call()
and subprocess.Popen()
. 我觉得我没有理解
subprocess.call()
和subprocess.Popen()
之间的概念或区别。
This is what I currently have: 这就是我目前拥有的:
def setup_device():
try:
output = open("file.txt", "w")
put_device_down = subprocess.call(["ifconfig", "wlan0", "down"])
put_device_mon = subprocess.call(["iwconfig", "wlan0", "mode", "monitor"])
put_device_up = subprocess.call(["iwconfig", "wlano", "up"])
start_device = subprocess.call(["airmon-ng", "start", "wlan0"])
scanned_networks = subprocess.Popen(["airodump-ng", "wlan0"], stdout = output)
time.sleep(10)
scanned_networks.terminate()
except Exception, e:
print "Error:", e
I am still clueless about where and when and in which way to use subprocess.call()
and subprocess.Popen()
我仍然对使用
subprocess.call()
和subprocess.Popen()
方式和时间以及使用方式一无所知
The thing that I think is confusing me most is the stdout
and stderr
args. 我认为让我最困惑的是
stdout
和stderr
args。 What is PIPE
? 什么是
PIPE
?
Another thing that I could possibly fix myself once I get a better grasp is this: 一旦我掌握得更好,我可以修复自己的另一件事是:
When running subprocess.Popen()
and running airodump, the console window pops up showing the scan. running subprocess.Popen()
并运行airodump时,会弹出控制台窗口,显示扫描。 Is there a way to hide this from the user to sort of clean things up? 有没有办法将这个隐藏起来?
You don't have to use Popen()
if you don't want to. 如果你不想,你不必使用
Popen()
。 The other functions in the module, such as .call()
use Popen()
, give you a simpler API to do what you want. 模块中的其他函数,例如
.call()
使用Popen()
,为您提供了一个更简单的API来完成您想要的任务。
All console applications have 3 'file' streams: stdin
for input, and stdout
and stderr
for output. 所有控制台应用程序都有3个'文件'流:
stdin
用于输入, stdout
和stderr
用于输出。 The application decides what to write where; 应用程序决定在哪里写; usually error and diagnostic information to
stderr
, the rest to stdout
. 通常是
stderr
错误和诊断信息,其余的是stdout
。 If you want to capture the output for either of these outputs in your Python program, you specify the subprocess.PIPE
argument so that the 'stream' is redirected into your program. 如果要在Python程序中捕获这些输出中的任何一个的输出,请指定
subprocess.PIPE
参数,以便将“stream”重定向到您的程序中。 Hence the name. 由此得名。
If you want to capture the output of the airodump-ng wlan0
command, it's easiest to use the subprocess.check_output()
function; 如果要捕获
airodump-ng wlan0
命令的输出,最简单的方法是使用subprocess.check_output()
函数; it takes care of the PIPE
argument for you: 它为您处理
PIPE
论证:
scanned_networks = subprocess.check_output(["airodump-ng", "wlan0"])
Now output
contains whatever airodump-ng
wrote to its stdout
stream. 现在
output
包含airodump-ng
写入其stdout
流的任何内容。
If you need to have more control over the process, then you do need to use the Popen()
class: 如果您需要对进程有更多控制权,那么您需要使用
Popen()
类:
proc = subprocess.Popen(["airodump-ng", "wlan0"], stdout=subprocess.PIPE)
for line in proc.stdout:
# do something with line
proc.terminate()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.