简体   繁体   中英

Run a Ruby Script with Python

I have a Ruby Script that when I run in the shell it works (as user):

/home/user/wpscan/$ ruby ./wpscan.rb -u www.mysite.com

However, I'd like to automate this with a function that I created with Python. Here is the python script:

#!/usr/bin/python

import os
wpscan_env = "/wpscan/"
os.chdir(os.environ['HOME'] + wpscan_env)
os.system("ruby ./wpscan.rb -u www.mysite.com")

Notice that the Python script is in a different folder, home/user/python/first.py and this is why I do the os.chdir() function. When I go back to the shell and type:

/home/user/python/$ python first.py

This is the output I get:

Could not find addressable-2.4.0 in any of the sources

Run `bundle install` to install missing gems.

I am using Ubuntu 14.04 and in order to get wpscan to work, it asked me to install Ruby 2.3.0. I did this via RVM.

wpscan.rb has a few dependencies and it seems it's not instantiating them. Also if I'm inside the wpscan folder and do ruby ./wpscan.rb ... it will work. However if I try to do this from the home directory: ruby wpscan/wpscan.rb ... it throws an error:

[ERROR] cannot load such file --typhoeus

[TIP] try to run 'gem install typhoeus' or 'gem install --user-install typhoeus'. If you still get an error, Please see README file or https://github.com/wpscanteam/wpscan

I have no knowledge of Ruby, this is my first real Python script, and I just installed wpscan 2 nights ago. I'm way out of my league here and I need help. Any further question can be elaborated per request.

Most probably, ruby in your python script is not the same as ruby in your shell. This can happen if you eg have a default system ruby installed (eg from the system packages) and you install another ruby version via RVM.

When using shell, the RVM automatically loads it's environment from the shell init scripts (eg ~/.bashrc ) and knows which ruby to use from its settings. Whereas your python script does not load any rvm environment (it's not a login shell) and calls the default system ruby.

In that case, you need to explicitly call the correct ruby from the RVM in your python script. You can to it by calling the RVM wrapper:

  • browse directories under ~/.rvm/wrappers/ and find the correct ruby version and gemset that you want to use
  • in your python script, call the ruby command from this wrapper directory instead of the plain `ruby, something like:
rvm_ruby = os.environ['HOME'] + "/.rvm/wrappers/ruby-2.3.0-p100@myproject/ruby"
os.system(rvm_ruby + " ./wpscan.rb -u www.mysite.com")

This should fix your problem.

Thats so simple to execute ruby scripts from python...

import os
os.system('ruby filename.rb')
#if you want to store the output in seperate file
os.system('ruby filename.rb > outfilename.txt)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM