0

My function runs URLcrazy and I figured out it is not storing the output into any variables and I cannot figure it out. The tmp variable shown is not storing the output into that list. How I can fix this to store the data executed.

def run_urlcrazy():
   tmp = []
    for domain in grab_domains():
      np = os.system("urlcrazy " + domain)
      tmp.append(np)
   return tmp

I am trying to get the output of URLcrazy ran against all the domains in the loop and be able to slice the ouptut. I think the way URLcrazy is outputting data I am unable to format the data in another function. Which I need to be able to do in order to store information in a database.

  • Are you sure that `grab_domains()` has any elements? – vishes_shell Jul 11 '17 at 19:33
  • The return value of [`os.system`](https://docs.python.org/2/library/os.html#os.system) is the process *return code*, not the process *output*. To capture output [see this question](https://stackoverflow.com/questions/923079/how-can-i-capture-the-stdout-output-of-a-child-process). – tadman Jul 11 '17 at 19:35

1 Answers1

1

os.system does not return the output of the command executed, only the exit code (signals success or error).

I believe you want subprocess.Popen:

import subprocess

def run_urlcrazy():
    tmp = []
    for domain in grab_domains():
        proc = subprocess.Popen(["urlcrazy", domain], stdout=subprocess.PIPE)
        (out, err) = proc.communicate()
        tmp.append(out)
   return tmp

Alternatively, you can use subprocess.check_output if you just want the output and are not interested in checking for errors:

out = subprocess.check_output(['urlcrazy', domain])
cs95
  • 379,657
  • 97
  • 704
  • 746
  • This seems the correct route, but it is not running urlcrazy with domain as the arguement anymore. what do you think could be the problem? – jakethesnake Jul 11 '17 at 20:25
  • @jakethesnake I believe the issue was that the first arg should not be a list when shell is set to True. I have edited my answer. It should work now. – cs95 Jul 11 '17 at 20:28
  • @COLDSPEED Hmmmm that cleared up the errors but it still is not running urlcrazy anymore with domain as arg. – jakethesnake Jul 11 '17 at 20:35
  • @jakethesnake Try `out = subprocess.check_output(['urlcrazy', domain])` – cs95 Jul 11 '17 at 20:38
  • That didnt not work either, I have urlcrazy in the same folder as my scripts. I don't get why it is not working. I appreciate your help! – jakethesnake Jul 11 '17 at 20:44
  • @jakethesnake Add it to your path and try again! – cs95 Jul 11 '17 at 20:46
  • @COLSPEED nope :( – jakethesnake Jul 11 '17 at 20:54
  • @jakethesnake Just tested both examples for normal bash commands and they work like a charm! I'm sorry you have this problem. Take a look [here](https://docs.python.org/2/library/subprocess.html#using-the-subprocess-module) and try out different options. One of em should work. – cs95 Jul 11 '17 at 20:58