1
votes

I would like to run the subprocess.check_output method for my python script.

try:
        logger.info('Loading URL ' + line)
        wp_output = subprocess.checkout(['ruby', PATH + '/wpscan.rb', '--url', line, '--enumerate',
            'vp', '--enumerate', 'vt'])
        print wp_output
        logger.info(wp_output)
        return wp_output.strip()

    except KeyboardInterrupt:
        raise
    except subprocess.CalledProcessError, e:
        logger.exception('ERROR - Problem occurred while using wpscan.')

the exception:

 File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 573, in check_output
    raise CalledProcessError(retcode, cmd, output=output)
CalledProcessError: Command '[u'ruby', u'./wpscan/wpscan.rb', u'--url', 'www.website.de', u'--enumerate', u'vp', u'--enumerate', u'vt']' returned non-zero exit status 1

if I run check_output I get a non-zero exception, if I use only "subprocess.call" I get as result "print wp_output" only the int 1??

What I am doing wrong? I would like to get the output as a string (wp_output).

2
Are you getting any error when you directly run that command from terminal? - Anand S Kumar
@AnandSKumar no, it works fine - Loretta
ruby wpscan.rb --url www.mywebsite.de --enumerate vp --enumerate vt - works perfect - Loretta
What is PATH variable ? - Anand S Kumar
config.ini [PATHS] path = ./wpscan - Loretta

2 Answers

1
votes

When you run subprocess.call is returns an int that represents the exit code of the program. Typically a 0 means everything ran fine, and other numbers, such as your 1, indicate an error.

subprocess.check_output will specifically treat an error as an exception within python itself, and raise it, which leads to your result.

If you want the string output whether it was an error or a success, use Popen.

command = ['ruby', PATH + '/wpscan.rb', '--url', line, '--enumerate',
        'vp', '--enumerate', 'vt']
wp_output = subprocess.Popen(command, stdout=subprocess.PIPE)
wp_output = wp_output.communicate()
print wp_output

Using Popen with subprocess.PIPE and then running communicate on it will gives you a tuple containing the text that your command returned, whether it's an error or not.

0
votes

This is an addon to SuperBiasedMan's comment. It will be nice to redirect the stderr too as errors usually appear in stderr than stdout.

command = ['ruby', PATH + '/wpscan.rb', '--url', line, '--enumerate',
    'vp', '--enumerate', 'vt']
wp_proc = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
wp_output, wp_error = wp_proc.communicate()

if wp_error != '':
    print wp_error
else:
    print wp_output