-1

I use these three Linux commands one after each other in terminal to enable monitoring mode on Raspberry Pi 3.

iw phy phy0 interface add mon0 type monitor

ifconfig mon0 up

airudump-ng -w mon0

I want to run this these commands in Python file instead of on terminal.

I have little idea about subprocess module but don't know how to do this.

Please suggest me a way to do this.

jww
  • 97,681
  • 90
  • 411
  • 885
Asma
  • 17
  • 7

3 Answers3

0

The code is either

import subprocess
subprocess.call(["iw", "phy", "phy0", "interface", "add", "mon0", "type", "monitor"])
subprocess.call(["ifconfig", "mon0", "up"])
subprocess.call(["airodump-ng", "-w", "mon0"])

or

import subprocess
subprocess.run(["iw", "phy", "phy0", "interface", "add", "mon0", "type", "monitor"])
subprocess.run(["ifconfig", "mon0", "up"])
subprocess.run(["airodump-ng", "-w", "mon0"])

The latter is suggested by the docs if you want to use the standard output/error.

See also this other answer.

Next time, maybe check if a similar answer already exists.

pittix
  • 161
  • 12
0

Pass the commands as a list to subprocess.Popen

import subprocess

subprocess.Popen(["iw", "phy", "phy0", "interface", "add", "mon0", "type", "monitor"])

subprocess.Popen(["ifconfig", "mon0", "up"])

subprocess.Popen(["airudump-ng", "-w", "mon0"])

If you need to wait for the command to finish use .wait or use subprocess.call

Edit: If you need to read the stdout, stderr and exit status, you could pipe them to subprocess.

p = subprocess.Popen([some cmd], stdout=subprocess.PIPE, stderr=subprocess.PIPE)

stdout,stderr = p.communicate()
exit_status = p.wait()
HariUserX
  • 1,341
  • 1
  • 9
  • 17
0

Create and edit a script file using vi editor on the terminal. Once the editor opens to edit type in the following commands as you require.

vi script.sh

iw phy phy0 interface add mon0 type monitor

ifconfig mon0 up

airudump-ng -w mon0

Save the file by hitting esc->w->q on your keyboard.

Now, if your script file's path is lets say, /home/user/script.sh, in the python code:

import subprocess
subprocess.call(["sh", "/home/user/script.sh"])
Akash
  • 100
  • 1
  • 10