我有两个ruby代码,一个是ssh.rb,另一个是generate_ip.rb,它们都在同一目录中。当我在generate_ip.rb中导入ssh.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)
generate_ip.rb↓
require './ssh.rb'
这是我的目录结构↓
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
它是完整的代码:
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)
我尝试require_relative
,它不起作用-,-
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(与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).
...
...
...
无论如何,我非常感谢所有能给我建议的人。
答案 0 :(得分:3)
您的问题不是require_relative 'ssh.rb'
中的generate_ip.rb
。
您的问题在ssh.rb
的第8行
2: from /root/ssh.rb:8:in '<top (required)>'
OP编辑了问题之后:
据我了解,您使用过net-ssh
宝石。
您需要install
gem install net-ssh
答案 1 :(得分:0)
请尝试以下操作:
require './ssh.rb'
与加载不同,require的加载路径中没有当前目录,因此请使用 require_relative 或使用圆点指定当前目录