1

I have two ruby code, one is ssh.rb and the other is generate_ip.rb, they are all in the same directory. When I import ssh.rb in generate_ip.rb, I always getting the following error:

Traceback (most recent call last):
    5: from generate_ip.rb:1:in `<main>'
    4: from /usr/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in `require'
    3: from /usr/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in `require'
    2: from /root/ssh.rb:8:in `<top (required)>'
    1: from /usr/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in `require'
/usr/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in `require': cannot load such file -- net/ssh/config (LoadError)

generate_ip.rb↓

require './ssh.rb'

this is my directory structure↓

root@BabyZe:~# ls -l
total 56
drwxr-xr-x 3 root root  4096 Apr  1 20:32 Desktop
drwxr-xr-x 2 root root  4096 Sep 27  2018 Documents
drwxr-xr-x 2 root root  4096 Sep 27  2018 Downloads
-rw-r--r-- 1 root root    32 May  3 07:36 generate_ip.rb
drwxr-xr-x 2 root root  4096 Sep 27  2018 Music
drwxr-xr-x 3 root root  4096 May  3 06:04 Notebooks
drwxr-xr-x 2 root root  4096 Sep 27  2018 Pictures
drwxr-xr-x 2 root root  4096 Sep 27  2018 Public
-rw-r--r-- 1 root root 15595 May  3 06:45 ssh.rb
drwxr-xr-x 2 root root  4096 Sep 27  2018 Templates
drwxr-xr-x 2 root root  4096 Sep 27  2018 Videos

it is a complete code:

root@BabyZe:~# ls -l
total 56
drwxr-xr-x 3 root root  4096 Apr  1 20:32 Desktop
drwxr-xr-x 2 root root  4096 Sep 27  2018 Documents
drwxr-xr-x 2 root root  4096 Sep 27  2018 Downloads
-rw-r--r-- 1 root root    19 May  3 07:48 generate_ip.rb
drwxr-xr-x 2 root root  4096 Sep 27  2018 Music
drwxr-xr-x 3 root root  4096 May  3 06:04 Notebooks
drwxr-xr-x 2 root root  4096 Sep 27  2018 Pictures
drwxr-xr-x 2 root root  4096 Sep 27  2018 Public
-rw-r--r-- 1 root root 15595 May  3 06:45 ssh.rb
drwxr-xr-x 2 root root  4096 Sep 27  2018 Templates
drwxr-xr-x 2 root root  4096 Sep 27  2018 Videos
root@BabyZe:~# cat generate_ip.rb 
require './ssh.rb'
root@BabyZe:~# ruby generate_ip.rb 
Traceback (most recent call last):
    5: from generate_ip.rb:1:in `<main>'
    4: from /usr/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in `require'
    3: from /usr/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in `require'
    2: from /root/ssh.rb:8:in `<top (required)>'
    1: from /usr/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in `require'
/usr/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in `require': cannot load such file -- net/ssh/config (LoadError)

i try require_relative, it doesn't work -,-

root@BabyZe:~# cat generate_ip.rb 
require_relative 'ssh.rb'
root@BabyZe:~# ruby generate_ip.rb 
Traceback (most recent call last):
    4: from generate_ip.rb:1:in `<main>'
    3: from generate_ip.rb:1:in `require_relative'
    2: from /root/ssh.rb:8:in `<top (required)>'
    1: from /usr/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in `require'
/usr/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in `require': cannot load such file -- net/ssh/config (LoadError)

ssh.rb(same as Net:SSH)↓

# Make sure HOME is set, regardless of OS, so that File.expand_path works
# as expected with tilde characters.
ENV['HOME'] ||= ENV['HOMEPATH'] ? "#{ENV['HOMEDRIVE']}#{ENV['HOMEPATH']}" : Dir.pwd

require 'logger'
require 'etc'

require 'net/ssh/config'
require 'net/ssh/errors'
require 'net/ssh/loggable'
require 'net/ssh/transport/session'
require 'net/ssh/authentication/session'
require 'net/ssh/connection/session'
require 'net/ssh/prompt'

module Net

  # Net::SSH is a library for interacting, programmatically, with remote
  # processes via the SSH2 protocol. Sessions are always initiated via
  # Net::SSH.start. From there, a program interacts with the new SSH session
  # via the convenience methods on Net::SSH::Connection::Session, by opening
  # and interacting with new channels (Net::SSH::Connection:Session#open_channel
  # and Net::SSH::Connection::Channel), or by forwarding local and/or
  # remote ports through the connection (Net::SSH::Service::Forward).

...
...
...

No matter what, I am very grateful to all those who can give me advice.

李雨泽
  • 83
  • 8
  • 1
    Possible duplicate of [What is the difference between require\_relative and require in Ruby?](https://stackoverflow.com/questions/3672586/what-is-the-difference-between-require-relative-and-require-in-ruby) – Michael Berkowski May 03 '19 at 14:28
  • Maybe you can try `require_relative` – Lukas Baliak May 03 '19 at 14:29
  • Either use `require_relative`, or load it as `require './ssh.rb'` where the `./` in `require` context is for the current directory – Michael Berkowski May 03 '19 at 14:30
  • thanks , The problem seems to be because I did not install Net::SSH via gem and copied its code directly to the current directory. – 李雨泽 May 03 '19 at 15:21

2 Answers2

4

Your problem is not require_relative 'ssh.rb' in generate_ip.rb.

Your problem is in line 8 of ssh.rb

2: from /root/ssh.rb:8:in '<top (required)>'

After the OP edited the question:

As I understand you've used net-ssh gem.

You need to install it

gem install net-ssh
mechnicov
  • 12,025
  • 4
  • 33
  • 56
  • Thank you for your answer, I copied the Net::SSH code directly to the directory, and I did not install Net::SSH via gem.it is something wrong ... – 李雨泽 May 03 '19 at 15:19
0

Please, try this:

require './ssh.rb'

Unlike load, require does not have current directory in its load path, so either use require_relative or use the dot to specify current directory

alt-ja
  • 144
  • 1
  • 11