require jruby seems to work and fail simultaneously - ruby

John-Breedloves-Mac-mini:~ john_breedlove$ irb
>> require 'jruby'
=> true
>> require 'zxing'
RuntimeError: ZXing requires JRuby
from /Library/Ruby/Gems/1.8/gems/zxing-0.1.1/lib/zxing.rb:1
from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rubygems/custom_require.rb:36:in `gem_original_require'
from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rubygems/custom_require.rb:36:in `require'
from (irb):2
>>
How is this possible?
Further, how would I write that in a ruby script, though?
I have a file I want to execute called test.rb, which contains the following:
require 'rubygems'
require 'jruby'
require 'zxing'
ZXing.decode 'test.png'
I am executing it from the command-line like so:
ruby test.rb
In this context, how do I include java? Or is this even possible?

You are using the JRuby gem, rather than the JRuby itself.
JRuby (which ZXing is checking on line 1) is a module that gets defined only after you require 'java' in JRuby.
This should be clearly stated in ZXing's documentation, but it doesn't seem to be.
Here's the test output when I run the following from cloned ZXing source code:
$ ruby -v -I lib -r zxing -e 'p 0'
ruby 1.8.7 (2009-06-12 patchlevel 174) [universal-darwin10.0]
./lib/zxing.rb:1: ZXing requires JRuby (RuntimeError)
$ jruby -I lib -r zxing -e 'p 0'
/Users/asari/Development/src/zxing.rb/lib/zxing.rb:1: ZXing requires JRuby (RuntimeError)
from /Users/asari/Development/src/zxing.rb/lib/zxing.rb:1
...internal jruby stack elided...
from (unknown).(unknown)(/Users/asari/Development/src/zxing.rb/lib/zxing.rb:1)
from (unknown).(unknown)(:1)
$ jruby -r java -I lib -r zxing -e 'p 0'
0

Related

Correct way to combine `bundle exec` and `ruby -r`?

Let's experiment with a random gem, it doesn't matter which.
# Gemfile
gem "hashie"
Let's install our bundle in a (semi) non-standard place, so that ruby can't find it:
bundle install --path=vendor/bundle
ruby -e 'require "hashie"'
# cannot load such file -- hashie (LoadError)
As expected, if we add bundle exec to the above, then ruby -e can find our gem:
bundle exec ruby -e 'puts require "hashie"'
# true
But, even with bundle exec, ruby -r cannot find our gem!
bundle exec ruby -r hashie -e 'puts "win"'
# cannot load such file -- hashie (LoadError)
The only way I have found to combine bundle exec and ruby -r is:
bundle exec ruby -r 'bundler/setup' -r hashie -e 'puts "win"'
# win
The documentation for bundle exec does not mention ruby -r, so I ask here, is this the correct way?
-r 'bundler/setup'
is acceptable. It means to "require all of the gems in my Gemfile".
'hashie' could not load its dependencies that is why "LoadError" occurred.
reference: bundler_setup

How do I import gems that have been downloaded when installing some software?

I am using Kali Linux, it has pre-installed a lot of software written in ruby (such as metasploit, beef), today I want to import a ssh gem when writing my own script, but I failed because it is fundamental It was not installed, but I saw that the software written in ruby also imported it. and it run well, How do they work?
Found in my system:
root#kali:/home# find / -type f -iname *ssh*.rb
/usr/share/metasploit-framework/modules/post/linux/manage/sshkey_persistence.rb
/usr/share/metasploit-framework/modules/post/multi/gather/ssh_creds.rb
/usr/share/metasploit-framework/modules/auxiliary/dos/windows/ssh/sysax_sshd_kexchange.rb
/usr/share/metasploit-framework/modules/auxiliary/scanner/ssh/ssh_login_pubkey.rb
/usr/share/metasploit-framework/modules/auxiliary/scanner/ssh/ssh_identify_pubkeys.rb
/usr/share/metasploit-framework/modules/auxiliary/scanner/ssh/ssh_enumusers.rb
/usr/share/metasploit-framework/modules/auxiliary/scanner/ssh/ssh_version.rb
/usr/share/metasploit-framework/modules/auxiliary/scanner/ssh/ssh_login.rb
/usr/share/metasploit-framework/modules/auxiliary/fuzzers/ssh/ssh_version_2.rb
/usr/share/metasploit-framework/modules/auxiliary/fuzzers/ssh/ssh_version_15.rb
/usr/share/metasploit-framework/modules/auxiliary/fuzzers/ssh/ssh_version_corrupt.rb
/usr/share/metasploit-framework/modules/auxiliary/fuzzers/ssh/ssh_kexinit_corrupt.rb
/usr/share/metasploit-framework/modules/exploits/linux/ssh/symantec_smg_ssh.rb
/usr/share/metasploit-framework/modules/exploits/linux/ssh/mercurial_ssh_exec.rb
/usr/share/metasploit-framework/modules/exploits/windows/ssh/freesshd_key_exchange.rb
/usr/share/metasploit-framework/modules/exploits/windows/ssh/freesshd_authbypass.rb
/usr/share/metasploit-framework/modules/exploits/windows/ssh/sysax_ssh_username.rb
/usr/share/metasploit-framework/modules/exploits/windows/ssh/securecrt_ssh1.rb
/usr/share/metasploit-framework/modules/exploits/apple_ios/ssh/cydia_default_ssh.rb
/usr/share/metasploit-framework/modules/exploits/multi/ssh/sshexec.rb
/usr/share/metasploit-framework/vendor/bundle/ruby/2.3.0/gems/dnsruby-1.60.2/test/tc_sshfp.rb
/usr/share/metasploit-framework/vendor/bundle/ruby/2.3.0/gems/dnsruby-1.60.2/lib/dnsruby/resource/SSHFP.rb
/usr/share/metasploit-framework/vendor/bundle/ruby/2.3.0/gems/rex-socket-0.1.10/lib/rex/socket/ssh_factory.rb
/usr/share/metasploit-framework/vendor/bundle/ruby/2.3.0/gems/net-ssh-4.2.0/lib/net/ssh.rb
/usr/share/metasploit-framework/vendor/bundle/ruby/2.3.0/gems/net-ssh-4.2.0/support/ssh_tunnel_bug.rb
/usr/share/metasploit-framework/vendor/bundle/ruby/2.3.0/gems/metasploit-credential-2.0.12/spec/models/metasploit/credential/ssh_key_spec.rb
/usr/share/metasploit-framework/vendor/bundle/ruby/2.3.0/gems/metasploit-credential-2.0.12/spec/factories/metasploit/credential/ssh_keys.rb
/usr/share/metasploit-framework/vendor/bundle/ruby/2.3.0/gems/metasploit-credential-2.0.12/app/models/metasploit/credential/ssh_key.rb
/usr/share/metasploit-framework/vendor/bundle/ruby/2.3.0/gems/metasploit-credential-2.0.12/db/migrate/20161107203710_create_index_on_private_data_and_type_for_ssh_key.rb
/usr/share/metasploit-framework/vendor/bundle/ruby/2.3.0/gems/sshkey-1.9.0/test/sshkey_test.rb
/usr/share/metasploit-framework/vendor/bundle/ruby/2.3.0/gems/sshkey-1.9.0/lib/sshkey.rb
/usr/share/metasploit-framework/lib/msf/core/exploit/ssh.rb
/usr/share/metasploit-framework/lib/metasploit/framework/login_scanner/ssh.rb
/usr/share/metasploit-framework/scripts/meterpreter/win32-sshclient.rb
/usr/share/metasploit-framework/scripts/meterpreter/win32-sshserver.rb
I also saw that they have a module that imports Net:SSH:
root#kali:/home# cat /usr/share/metasploit-framework/modules/auxiliary/scanner/ssh/ssh_login.rb
##
# This module requires Metasploit: https://metasploit.com/download
# Current source: https://github.com/rapid7/metasploit-framework
##
require 'net/ssh'
require 'net/ssh/command_stream'
require 'metasploit/framework/login_scanner/ssh'
require 'metasploit/framework/credential_collection'
...
...
...
but when i use it:
root#kali:/home# irb
irb(main):001:0> require 'net/ssh'
LoadError: cannot load such file -- net/ssh
from /usr/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /usr/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from (irb):1
from /usr/bin/irb:11:in `<main>'
irb(main):002:0>
Can I use it in my script without using gem install?
Anyway, thank those who gave me advice, I hope you have a happy day~
You can either set GEM_PATH environment variable (with ruby or irb):
GEM_PATH=/usr/share/metasploit-framework/vendor/bundle/ruby/2.3.0/ ruby -e 'require "net/ssh"'
Or you can set it in the code of your script:
#!/usr/env/ruby
Gem.paths = { 'GEM_HOME' => "/usr/share/metasploit-framework/vendor/bundle/ruby/2.3.0/" }
require 'net/ssh'

byebug and ruby 2.2.3

When trying to use byebug for the first time with ruby 2.2.3 I get
NameError:
undefined local variable or method `byebug'
I'm confused, here's are some diagnostics:
ruby --version
ruby 2.2.3p173 (2015-08-18 revision 51636) [x86_64-darwin14]
cat Gemfile | grep bye
gem 'byebug'
gem list | grep bye
byebug (8.2.1, 8.2.0)
#the command I'm using to run my ruby code is
rspec *spec.rb --tag focus
#also tried prepending bundle exec
Figure it out. I needed this line:
require 'byebug'; byebug;
in my ruby code.
did you run bundle install on your app (assuming this is in the context of a rails app given that you're using Gemfiles)?
Edit: you figured it out, I see :D I was going to suggest requiring the library next but I see that I didn't need to.

ruby command line LoadError

When I try to include a library on the command line, I receive LoadError messages
$ ruby -v
ruby 1.8.7 (2012-06-29 patchlevel 370)
$ gem list | grep coderay_bash
coderay_bash (1.0.2)
$ ruby -rcoderay_bash /bin/coderay -v
ruby: no such file to load -- coderay_bash (LoadError)
$ ruby -rubygems -rcoderay_bash /bin/coderay -v
ruby: no such file to load -- coderay_bash (LoadError)
It looks to work with ruby 1.9.2
$ ruby -v
ruby 1.9.2p290 (2011-07-09)
$ ruby -rcoderay_bash /bin/coderay -v
CodeRay 1.0.7
In Ruby 1.8, anything you want to require that was installed with RubyGems cannot be accessed until you require 'rubygems'. 1.9 removes this requirement.
You have several options for this:
Just put require 'rubygems' at the top of your file. This is harmless for 1.9 and is probably the easiest thing, because it's in the code and no one using your app has to remember anything
Change your shebang line to #!/usr/bin/env ruby -rubygems This tells the Ruby interpreter to require rubygems, but allows users to avoid this by sending your file to ruby directly, if they are offended by RubyGems for some reason
Always run with ruby and use -rubygems, e.g. ruby -rubygems my_app.rb This has no dependencies on RubyGems in your code, and will work, but you have to remember to do it everytime, which is somewhat of a pain.

Install non-gem library in Ruby

How do you install a library that is not a gem in Ruby?
I'm trying to use graphy.
In the example usage, it says to require 'graphy', but even when my ruby file is in the same directory as graphy.rb, I get the following error:
<internal:lib/rubygems/custom_require>:29:in `require': no such file to load -- graphy.rb (LoadError)
from <internal:lib/rubygems/custom_require>:29:in `require'
from foo.rb:1:in `<main>'
Tell ruby to look in the current dir using the -I flag:
ruby -I. my_script.rb
To see the ruby load path, add puts $: at the top of your script.
Everything works fine. I have pasted my IRB try below.
Yasky$ cd Projects/ruby/bruce-graphy-70f213b/lib/
lib Yasky$ irb
ruby-1.8.7-p352 :001 > require 'graphy'
=> true
ruby-1.8.7-p352 :002 > dg = Graphy::Digraph[1,2, 2,3, 2,4, 4,5, 6,4, 1,6]
=> Graphy::DirectedGraph[Graphy::Arc[2,3,nil], Graphy::Arc[1,6,nil], Graphy::Arc[2,4,nil], Graphy::Arc[4,5,nil], Graphy::Arc[1,2,nil], Graphy::Arc[6,4,nil]]
ruby-1.8.7-p352 :003 > exit
lib Yasky$
Explicitly specifying your current directory in your load path may do the trick.
EDIT: Oops, I was too late (:
In this particular case, the author is using Jeweler to manage his gemspec. I'm not that familiar with Jeweler, but AFAIR, you generate and install a Gem with rake install. You may need to generate a version number first with rake version:write MAJOR=0 MINOR=0 PATCH=1.

Resources