1

I am trying to grab the stdout from airodump-ng using subprocess with no luck. I think my code causes a deadlock.

   airodump = subprocess.Popen(['airodump-ng','mon0'],stdin=subprocess.PIPE,
                              stdout=subprocess.PIPE,
                              stderr=subprocess.PIPE)

    # wait for 15 seconds to find all networks
    time.sleep(15)

    # save the output
    o_airodump = airodump.stdout.read()
    os.kill(airodump.pid, signal.SIGKILL)
    # airodump.terminate
    print(o_airodump)

How to avoid this problem. Cant think one clean solution.

kechap
  • 2,077
  • 6
  • 28
  • 50
  • Why is stdin marked as a pipe? That might be causing problems. – Dubslow Jul 10 '12 at 09:54
  • stdin being a pipe won't matter, airodump-ng isn't expecting any input. The hang is caused by two things that my answer below addresses: The child processes can blocking due to filling up its output pipe buffers which aren't being read if it generates enough output *and* the unbounded read() call is not going to return until the process dies. – gps Jul 10 '12 at 18:21

2 Answers2

2

Don't sleep and wait (that will just cause airodump to block on a full pipe buffer) and don't use an unbounded read(). The communicate() method does what you need:

o_airodump, unused_stderr = airodump.communicate(timeout=15)
airodump.kill()

Note: The timeout parameter on communicate was introduced in Python 3.3 which isn't quite out yet. ;)

gps
  • 1,360
  • 12
  • 12
  • 1
    Thanks for the answer. I knew I was in the wrong direction. Does `timeout` need a special import? cause I get an error. I am using python3 if this makes a difference. – kechap Jul 07 '12 at 18:18
  • Updated to mention that, yes, timeout is new in Python 3.3. – gps Jul 09 '12 at 16:12
1
airodump.communicate() 

waits for the process to terminate then returns (stdout, stderr)

IF you really pushed you could always link directly to the c library using ctypes. Enjoy hacking.

Jakob Bowyer
  • 33,878
  • 8
  • 76
  • 91