Python Forum
Linux command output not working
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Linux command output not working
#1
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
Reply
#2
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.
My code examples are always for Python >=3.6.0
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to input & output parameters from command line argument shantanu97 1 556 Apr-13-2021, 02:12 PM
Last Post: Larz60+
  how to run linux command with multi pipes by python !! evilcode1 2 483 Jan-25-2021, 11:19 AM
Last Post: DeaD_EyE
  Print output not working xninhox 7 1,047 Jan-16-2021, 09:42 AM
Last Post: xninhox
  Subprocess command working for one cmd and for cmd one not wrking PythonBeginner_2020 0 2,589 Mar-25-2020, 01:52 PM
Last Post: PythonBeginner_2020
  can we write command output to new csv file using Panda package? PythonBeginner_2020 3 973 Mar-13-2020, 12:38 PM
Last Post: ndc85430
  execute linux command with arguments sivareddy 2 705 Feb-10-2020, 03:23 PM
Last Post: sivareddy
  Running linux command line apps... dbrdh 0 686 Jan-30-2020, 01:14 PM
Last Post: dbrdh
  Long command with characters not working in Python on Oracle Linux 7 iaas_infra 10 2,578 Jul-19-2019, 04:53 PM
Last Post: ichabod801
  A question about subprocess taking input from command line and returning output! Aurimas 8 2,359 May-15-2019, 04:02 PM
Last Post: Aurimas
  t.onkey command isn't working PythonSnake 2 1,416 Jan-24-2019, 08:02 PM
Last Post: PythonSnake

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020