何时使用 subprocess.call() 或 subprocess.Popen(),运行 airodump [英] When to use subprocess.call() or subprocess.Popen(), running airodump

查看:42
本文介绍了何时使用 subprocess.call() 或 subprocess.Popen(),运行 airodump的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个小脚本,可以让您的无线设备进入监控模式.它执行 airodump 扫描,然后在终止扫描后将输出转储到 file.txt 或变量,这样我就可以抓取 BSSID 和我可能需要的任何其他信息.

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.

我觉得我没有掌握subprocess.call()subprocess.Popen()之间的概念或区别.

I feel I haven't grasped the concept or difference between subprocess.call() and subprocess.Popen().

这是我目前拥有的:

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

我仍然不知道在何时何地以何种方式使用 subprocess.call()subprocess.Popen()

I am still clueless about where and when and in which way to use subprocess.call() and subprocess.Popen()

我认为最让我困惑的是 stdoutstderr 参数.什么是PIPE?

The thing that I think is confusing me most is the stdout and stderr args. What is PIPE?

一旦我更好地掌握,我可以解决的另一件事是:

Another thing that I could possibly fix myself once I get a better grasp is this:

running subprocess.Popen() 并运行 airodump 时,控制台窗口会弹出显示扫描.有没有办法对用户隐藏它以进行清理?

When running subprocess.Popen() and running airodump, the console window pops up showing the scan. Is there a way to hide this from the user to sort of clean things up?

推荐答案

如果您不想,您不必使用 Popen().模块中的其他函数,例如 .call() 使用 Popen(),给你一个更简单的 API 来做你想做的事.

You don't have to use Popen() if you don't want to. The other functions in the module, such as .call() use Popen(), give you a simpler API to do what you want.

所有控制台应用程序都有 3 个文件"流:stdin 用于输入,stdoutstderr 用于输出.应用程序决定在哪里写什么;通常错误和诊断信息到stderr,其余的到stdout.如果要在 Python 程序中捕获这些输出中的任何一个的输出,请指定 subprocess.PIPE 参数,以便将流"重定向到您的程序中.因此得名.

All console applications have 3 'file' streams: stdin for input, and stdout and stderr for output. The application decides what to write where; usually error and diagnostic information to stderr, the rest to 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. Hence the name.

如果您想捕获 airodump-ng wlan0 命令的输出,最简单的方法是使用 subprocess.check_output() 函数;它会为您处理 PIPE 参数:

If you want to capture the output of the airodump-ng wlan0 command, it's easiest to use the subprocess.check_output() function; it takes care of the PIPE argument for you:

scanned_networks = subprocess.check_output(["airodump-ng", "wlan0"])

现在 output 包含 airodump-ng 写入其 stdout 流的任何内容.

Now output contains whatever airodump-ng wrote to its stdout stream.

如果您需要对过程有更多的控制,那么您确实需要使用 Popen() 类:

If you need to have more control over the process, then you do need to use the Popen() class:

proc = subprocess.Popen(["airodump-ng", "wlan0"], stdout=subprocess.PIPE)
for line in proc.stdout:
    # do something with line
proc.terminate()

这篇关于何时使用 subprocess.call() 或 subprocess.Popen(),运行 airodump的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆