简体   繁体   English

何时使用subprocess.call()或subprocess.Popen(),运行airodump

[英]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. 我认为让我最困惑的是stdoutstderr 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用于输入, stdoutstderr用于输出。 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.

相关问题 如果可能,应该避免使用subprocess.Popen和subprocess.call吗? - Should the use of subprocess.Popen and subprocess.call be avoided if possible? subprocess.call()或subprocess.Popen用于生成/使用输出文件 - subprocess.call() or subprocess.Popen for generating/using output files Python:subprocess.Popen和subprocess.call挂起 - Python: subprocess.Popen and subprocess.call hang os.system到subprocess.Popen或subprocess.call - os.system to subprocess.Popen or subprocess.call Python subprocess.call线程挂起,subprocess.popen不挂起 - Python subprocess.call thread hang, subprocess.popen no hang subprocess.Popen执行.sh的方式与subprocess.call不同 - subprocess.Popen executes .sh differently then subprocess.call Python:ulimit和subprocess.call / subprocess.Popen很好用? - Python: ulimit and nice for subprocess.call / subprocess.Popen? Subprocess.call 或 Subprocess.Popen 不能使用 PATH (Linux/Windows) 中的可执行文件 - Subprocess.call or Subprocess.Popen cannot use executables that are in PATH (Linux/Windows) sp = subprocess.Popen()-> sp.communicate()与subprocess.call()的优点 - advantages of sp = subprocess.Popen() --> sp.communicate() vs subprocess.call() Python subprocess.Popen()没有显示正确的stdout subprocess.call() - Python subprocess.Popen() doesn't show correct stdout subprocess.call()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM