Python Forum
Linux command output not working - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Linux command output not working (/thread-16972.html)



Linux command output not working - adam2020 - Mar-22-2019

Hi guys,

I am working on a network analysis tool for a project in college, I am using airodump-ng to find all networks in the general area,

when I run the following command airodump-ng wlan0 --write tempfile --output-format' in the python script nothing gets created but when I run it manually a csv file with the output will be created

from subprocess import Popen,PIPE
import sys,os
import csv        

        def execute_command(command):
        process = Popen(command,stdout=PIPE,stder=PIPE)
        return process.communicate()


tempfile = "\root\Desktop\networks
# instance of WPA class test_WPA created, only execute_command method is of relevance


out,err = test_WPA.execute_command(['airodump-ng','wlan0','--write',tempfile,'--output-format','csv'])

it does seem to be working now after I put my wireless card in

I changed the code a bit and made airodump-ng run in another thread

command = ['airodump-ng','wlan0','--write',tempfile,'--output-format','csv']
thread = threading.Thread(target= test_WPA.execute_command,args= (command, ))
thread.start()
thread.join(1)
the great thing it now creates the file, but the the process remains open I want the airodump-ng(thread) to stop running after 30 seconds, should set thread to be a daemon thread?? or is there another way I can get airodump to stop running after 30 seconds?

thanks


RE: Linux command output not working - DeaD_EyE - Mar-23-2019

No threads needed

import time


def execute_command(command):
        process = Popen(command,stdout=PIPE,stder=PIPE)
        time.sleep(10)
        process.terminate()
The call communicate blocks until the process has been finished.
If you don't terminate the process, then it's running in the background and does not block.
In the case of a long running background process, you should return proc in your function.


RE: Linux command output not working - adam2020 - Mar-23-2019

thanks DeadEye

to get the output could I still do vv

  
  time.sleep(10)
  stdout,stderr = process.communicate()
  process.terminate()
  return stdout,stderr
this way I can still get the output from process.communicate() and return stdout and stderr respective outputs and also terminate the process