I'm relatively new to Ruby, and I have to use the version 1.8.7.
I have tried 'zip' and ended up with a cryptic stack trace while attempting to use the example from the docs. My script
#!/usr/bin/env ruby
require 'rubygems'
require 'zip'
require 'zip/zipfilesystem'
Zip::ZipFile.open("myfile.war") do |a|
puts a.file.read("META-INF/context.xml")
end
produces the following:
$./rzip.rb
/var/lib/gems/1.8/gems/zip-2.0.2/lib/zip/zip.rb:1137:in `dup': can't dup NilClass (TypeError)
from /var/lib/gems/1.8/gems/zip-2.0.2/lib/zip/zip.rb:1137:in `dup'
from /var/lib/gems/1.8/gems/zip-2.0.2/lib/zip/zip.rb:1137:in `map'
from /var/lib/gems/1.8/gems/zip-2.0.2/lib/zip/zip.rb:1137:in `dup'
from /var/lib/gems/1.8/gems/zip-2.0.2/lib/zip/zip.rb:1373:in `initialize'
from /var/lib/gems/1.8/gems/zip-2.0.2/lib/zip/zip.rb:1384:in `new'
from /var/lib/gems/1.8/gems/zip-2.0.2/lib/zip/zip.rb:1384:in `open'
from ./rzip.rb:7
What is wrong with my code? Or is it some version incompatibility issue? If it is not my blunder, what should I use for zip archives manipulations?
I can't reproduce your error although it seems you are not alone in this. Can I suggest using Zip::ZipFile. This is running on my system (Ubuntu 12.04 LTS):
require 'zip/zip'
Zip::ZipFile.open("my.zip", Zip::ZipFile::CREATE) {
|zipfile|
zipfile.get_output_stream("first.txt") { |f| f.puts "Hello from ZipFile" }
zipfile.mkdir("a_dir")
}
You can also see see some good examples/usage of Zip::ZipFile on GitHub
Related
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'
I have a ruby extension written in C++, P4, and it seems to generally work:
I can run irb -Ilib and then require 'P4', and use it
I can execute tests via rake by accessing the shell script in the bin folder of the rake gem, e.g., ${GEM_HOME}/gems/rake-10.3.2/bin/rake test
however, when I access rake via the RubyGems wrapper in my path, e.g., rake test, I get this TypeError
/Users/tjuricek/dev/p4ruby/lib/P4.rb:38:in `require': P4 is not a class (TypeError)
from /Users/tjuricek/dev/p4ruby/lib/P4.rb:38:in `<top (required)>'
from /Users/tjuricek/dev/p4ruby/test/testlib.rb:31:in `require'
from /Users/tjuricek/dev/p4ruby/test/testlib.rb:31:in `<top (required)>'
from /Users/tjuricek/.rvm/gems/ruby-2.1.2/gems/rake-10.3.2/lib/rake/rake_test_loader.rb:15:in `require'
from /Users/tjuricek/.rvm/gems/ruby-2.1.2/gems/rake-10.3.2/lib/rake/rake_test_loader.rb:15:in `block in <main>'
from /Users/tjuricek/.rvm/gems/ruby-2.1.2/gems/rake-10.3.2/lib/rake/rake_test_loader.rb:4:in `select'
from /Users/tjuricek/.rvm/gems/ruby-2.1.2/gems/rake-10.3.2/lib/rake/rake_test_loader.rb:4:in `<main>'
rake aborted!
Then it pops out the ruby command that "failed". If I copy and paste that command and run it, it works.
What I've noticed is that RubyGems creates a fairly simple wrapper script:
#!/usr/bin/env ruby_executable_hooks
#
# This file was generated by RubyGems.
#
# The application 'rake' is installed as part of a gem, and
# this file is here to facilitate running it.
#
require 'rubygems'
version = ">= 0"
if ARGV.first
str = ARGV.first
str = str.dup.force_encoding("BINARY") if str.respond_to? :force_encoding
if str =~ /\A_(.*)_\z/ and Gem::Version.correct?($1) then
version = $1
ARGV.shift
end
end
gem 'rake', version
load Gem.bin_path('rake', 'rake', version)
I'm guessing that the last line, load Gem.bin_path... has triggered some kind of misconfiguration of my part in creating my extension, but I have no idea what that would be.
Does anyone have ideas on what might cause require to fail only when run under the RubyGems wrapper?
OK, the way to debug this is to go into a line where you load the file:
require 'P4.so'
And see if it's defined:
puts "P4 #{P4.class}"
require 'P4.so'
In this case, when running under rake directly, it loaded the .gemspec which pulled in a version definition that created (incorrectly, in my case) a P4 module:
module P4
version = VERSION = '3000.0.0.pre0'
end
So, for me the fix included:
Changing module P4 to class P4
Requiring the version definition before my require 'P4.so' statement, typically: require_relative 'P4/version'
Not defining the class in my C++ extension code, but loading it and extending the one defined in my version file:
// Ensure the class has been defined by the version specification
cP4 = rb_path2class("P4");
I'm using:
Rails 3.0.7 and
Rspec 2.5.0
via rvm
When I run this spec (using autotest or bundle exec autotest or bundle exec rspec spec/) below:
require 'spec_helper'
require 'yaml'
def twitter_feed(id=1)
ruby_object = YAML.load_file(::Rails.root.to_s + "/spec/fixtures/feeds/twitter_response_#{id}.yml")
end
I get this:
Failure/Error: ruby_object = YAML.load_file(::Rails.root.to_s + "/spec/fixtures/feeds/twitter_response_#{id}.yml")
TypeError:
invalid subclass
# ./spec/models/tweet_spec.rb:6:in `twitter_feed'
# /Users/natebean/.rvm/rubies/ruby-1.8.7-p302/lib/ruby/1.8/yaml.rb:133:in `transfer'
# /Users/natebean/.rvm/rubies/ruby-1.8.7-p302/lib/ruby/1.8/yaml.rb:133:in `node_import'
# /Users/natebean/.rvm/rubies/ruby-1.8.7-p302/lib/ruby/1.8/yaml.rb:133:in `load'
# /Users/natebean/.rvm/rubies/ruby-1.8.7-p302/lib/ruby/1.8/yaml.rb:133:in `load'
# /Users/natebean/.rvm/rubies/ruby-1.8.7-p302/lib/ruby/1.8/yaml.rb:144:in `load_file'
# /Users/natebean/.rvm/rubies/ruby-1.8.7-p302/lib/ruby/1.8/yaml.rb:143:in `open'
# /Users/natebean/.rvm/rubies/ruby-1.8.7-p302/lib/ruby/1.8/yaml.rb:143:in `load_file'
# ./spec/models/tweet_spec.rb:5:in `twitter_feed'
# ./spec/models/tweet_spec.rb:58
This "was" working. I can't find any other information on this error on the internet. I've moved from rails 3.0.3 to 3.0.7, but don't remember it not working after the upgrade.
Any suggestions? Thanks.
The yaml file I was pulling was looking for Hashie::Mash to map the data to. Up to know I didn't need to require 'hashie', but that has "fixed this problem".
I added to this to my spec and it is now working.
require 'hashie'
Run bundle exec rspec spec --backtrace to get a full backtrace so you can see exactly where that error is coming from.
I'm trying to write a Ruby plasmoid for KDE. I need to use barely one rubygem. Whenever I write require 'dbus', it throw me and an error:
code/main.rb:6:in 'require': no such file to load -- dbus (LoadError)
code/main.rb:6:in '<module:TestApp>'
code/main.rb:5:in '<top (required)>'
/usr/share/apps/plasma_scriptengine_ruby/applet.rb:177:in 'load'
/usr/share/apps/plasma_scriptengine_ruby/applet.rb:177:in 'init'
/usr/share/apps/plasma_scriptengine_ruby/applet.rb:201:in 'constraintsEvent': undefined method 'constraintsEvent' for nil:NilClass (NoMethodError)
/usr/share/apps/plasma_scriptengine_ruby/applet.rb:201:in 'constraintsEvent': undefined method 'constraintsEvent' for nil:NilClass (NoMethodError)
Actually, normal "ruby main.rb" works well (regarding on "require" part), but testing plasmoid with "plasmoidviewer" fails. Note, that regular gems from standart Ruby installation works well, i.e. require 'Qt4' or require 'yaml' loads perfectly. I'm using Ruby 1.9.2p180 under Linux.
09:40 PM - UPDATE: Richard Dale, one of the QtRuby developers, just fixed this issue a few minutes ago. Next release of KDE will have patched version of QtRuby.
require 'find'
require 'findUtils'
Find.find(PATH_WHERE_GEM_IS_INSTALLED) do |path|
if FileTest.directory?(path)
$: << File.expand_path(path)
if File.basename(path)[0] == ?. and File.basename(path) != '.'
Find.prune
else
next
end
else
end
end
and after that you can do
require 'dbus'
Have you tried this:
require 'rubygems'
?
I am writing the following code in irb in my windows terminal having ruby-1.9.2...
and even though the code is just using the find function of the module Find, I am encountering the following error:
irb(main):001:0> require 'find'
=> true
irb(main):002:0> Find.find("") do |f|
irb(main):003:1* p f.to_s
irb(main):004:1> end
Errno::ENOENT: No such file or directory
from C:/Ruby192/lib/ruby/1.9.1/find.rb:38:in `block in find'
from C:/Ruby192/lib/ruby/1.9.1/find.rb:38:in `collect!'
from C:/Ruby192/lib/ruby/1.9.1/find.rb:38:in `find'
from (irb):2
from C:/Ruby192/bin/irb:12:in `<main>'
I have also tried the above code in a file(.rb) but am encountering the same error.
Thanks in advance...
You're passing an empty string to the .find method. You're supposed to pass the paths you want to traverse as an argument, for example Find.find("/tmp") do |f|.
See the Find module's documentation for an example.