6

I am trying to create a python program that can periodically poll the output from airodump-ng, a wifi sniffing tool. I am doing this on a RPI running Raspbian and Python 3.4 I've looked up how to do this on several website but whenever I try this I get a sort of deadlock and the program stalls.

I am using this code:

import subprocess
airodump = subprocess.Popen(['sudo','airodump-ng','mon0'])
out,err = airodump.communicate(timeout=10)

So the weird thing is that when I type these commands one by one into IDLE running on the RPI, everything works but after 10 seconds I get a timeout error. When not using the timeout argument, the program simply stalls. Using the extra argument 'stdout=subprocess.PIPE' also doesn't work. But when I go to terminal and start up python using the command 'python3' and then typing in the first and second line, the whole screen is then filled with the output from airodump-ng and I cannot type anything anymore!

So how can I solve this? I just want to get the most recent output from airodump-ng and the output of airodum-ng can simply be updated in the background, in another thread. I just want the most recent output.

Héctor van den Boorn
  • 1,218
  • 13
  • 32
  • related: [Constantly print Subprocess output while process is running](http://stackoverflow.com/q/4417546/4279) – jfs Nov 26 '15 at 11:30

2 Answers2

2

You can use pyrcrack, a python aircrack-ng bindings.

PyrCrack is a Python API exposing a common aircrack-ng API. As AircrackNg will run in background processes, and produce parseable output both in files and stdout, the most pythonic approach are context managers, cleaning up after.

Installation:

This library is available on Pypi, you can install it directly with pip:

pip install pyrcrack

Usage:

This library exports a basic aircrack-ng API aiming to keep always a small readable codebase.

This has led to a simple library that executes each of the aircrack-ng’s suite commands and auto-detects its usage instructions. Based on that, it dinamically builds classes inheriting that usage as docstring and a run() method that accepts keyword parameters and arguments, and checks them BEFORE trying to run them.

Some classes expose themselves as async iterators, as airodump-ng’s wich returns access points with its associated clients.

You can have a look at the examples/ folder for some usage examples, such as the basic “scan for targets”, that will list available interfaces, let you choose one, put it in monitor mode, and scan for targets updating results each 2 seconds.

import asyncio

import pyrcrack

from rich.console import Console
from rich.prompt import Prompt


async def scan_for_targets():
    """Scan for targets, return json."""
    console = Console()
    console.clear()
    console.show_cursor(False)
    airmon = pyrcrack.AirmonNg()

    interface = Prompt.ask(
        'Select an interface',
        choices=[a['interface'] for a in await airmon.interfaces])

    async with airmon(interface) as mon:
        async with pyrcrack.AirodumpNg() as pdump:
            async for result in pdump(mon.monitor_interface):
                console.clear()
                console.print(result.table)
                await asyncio.sleep(2)


asyncio.run(scan_for_targets())
arVahedi
  • 107
  • 1
  • 3
  • 15
1

see the doc, works as intended, especially the Note

If the process does not terminate after timeout seconds, a TimeoutExpired exception will be raised. Catching this exception and retrying communication will not lose any output.

Note

The data read is buffered in memory, so do not use this method if the data size is large or unlimited.

I would look at wifite code which make extensive use of airodump !

euri10
  • 2,446
  • 3
  • 24
  • 46
  • so basically just let airodump-ng write its output to a csv file and read this file then via python? It gets the job done but feels a bit hacky... – Héctor van den Boorn Nov 25 '15 at 18:52
  • it does, I believe you when you said Using the extra argument 'stdout=subprocess.PIPE' also doesn't work but reading http://stackoverflow.com/questions/16882112/when-to-use-subprocess-call-or-subprocess-popen-running-airodump/16882176#16882176 it seems it does the job, doesn't it ? what do you mean by it doesn't work ? – euri10 Nov 25 '15 at 19:40
  • When I try that code and try to run that for-each loop; it stalls as the input has not terminated :( – Héctor van den Boorn Nov 26 '15 at 09:02