I'm trying to log into a remote machine using SSH in my Ruby code and then trying to cd into another directory once I SSHed in. Here is my Ruby code to do it
require 'net/ssh'
require 'rubygems'
require 'needle'
Net::SSH.start('host', 'shui', :password => "password") do |ssh|
output = ssh.exec!("hostname")
shell = ssh.shell.open
shell.pwd
shell.cd "/svn"
puts shell.pwd
end
when I run my script, it gives me this error:
testssh.rb:8:in `block in <main>': undefined method `shell' ... (NoMethodError)
I'm pretty sure I've installed the gems that are needed to do this. Any suggestions on how to fix this? Thanks!
There is no shell method in Net:SSH v2, the yielded object is a Net::SSH::Connection::Session.
This net-ssh extension gives you the feature you are looking for: https://github.com/mitchellh/net-ssh-shell
Can you check your version?
gem list --local | grep ssh
i have 2.5.2
I found multiple references online, but you should use:
http://net-ssh.rubyforge.org/ssh/v2/api/classes/Net/SSH.html
here is the reference to v1
http://net-ssh.github.com/ssh/v1/chapter-5.html
the later, uses the shell method. But i don't think you have that gem installed.
So have a look at the 1st one (v2). Thats working fine for me.
Related
I am a complete noob to Ruby. I wish to execute a file that I have taken from an Amazon AWS IoT page, but I have problems with the initial require as below.
I am using Ubuntu 18.04, I have installed ruby with sudo apt-get install ruby-full , and also sudo gem install mqtt . The file is called iot-connector.rb. I execute the file with ruby iot-connector.rb . It's obvious that it cannot find these require files, but how do I tell Ruby to find them. Thanks.
require ‘rubygems’
require ‘mqtt’
#more code....
The error I get :
Traceback (most recent call last):
iot-connector.rb:1:in `<main>': undefined local variable or method `‘rubygems’'
for main:Object (NameError)
You use wrong type of quotes, use regular ones instead of backquotes:
require 'rubygems'
require 'mqtt'
Backquotes are used to run shell commands, btw. it's quite strange you get ruby errors here, I'd expect sth like:
[29] pry(main)> `rubygems`
Errno::ENOENT: No such file or directory - rubygems
I am new to ruby and am trying to use the line command:
gem install shopify_theme
However, I get an error
NameError: undefined local variable or method 'shopify_theme' for main:Object
from <rb>:2
from C:/Ruby21/bin/irb:11:in '<main>'
I assume this has something to do with my local files? I've used Dir.chdir to target my folder. Also, I have the gem file here as well.
ref: https://github.com/Shopify/shopify_theme
Thank you and any help is greatly appreciated!
You may be in the ruby console. Exit the ruby console and use cmd.exe!
I just finished setting up another development server for my API built using Ruby and Sinatra, however on this server I can't get the curl gem to work properly.
I've installed libcurl and libcurl-devel, and installed the curl gem without any errors, but when I try to use it in code, it always fails. Below is an example in irb:
irb(main):001:0> require 'curl'
=> true
irb(main):002:0> http = Curl.get("http://www.mysuperawesomeapi.com/someendpoint") do|http|
irb(main):003:1* http.headers['accept'] = 'application/JSON'
irb(main):004:1> end
NameError: uninitialized constant Curl
from (irb):2
from /usr/bin/irb:11:in `<main>'
The difference between this development server, and the other one is that this one is using Fedora 21 32bit (hardware limitation) while the other is using CentOS 7 64bit and is a virtual machine. When I try the same code above on irb on the CentOS VM, it works as expected. Any insight would be greatly appreciated.
It looks like in curl (unlike as in curb), there is a class CURL, but not Curl.
Inside gemfile include:
gem 'curl'
gem 'curb'
Anyone know why I would get this error: " NameError: undefined local variable or method `list' for main:Object
from (irb):1"
anytime i try to run a ruby command such as "gem list" from irb?
excuse my ignorance , ik nothing about Ruby and all im trying to do is install cocoapods to use with my ios projects.
It's because gem list is not "ruby command". It's shell command to list all ruby gems installed on your machine. You should use this outside ruby's irb console.
I am experiencing issues when I am trying to run my .rb-file with the Ruby-command trying to access a gem. The gem i am trying to use is Ruby-Whois. I have an example script below that when I try to execute it through "ruby whois.rb" I get this error message:
./whois.rb:6: uninitialized constant Whois (NameError)
However, if I run the same script line by line in IRB I get the expected result. What may cause this?
Below is whois.rb
require "rubygems"
require "whois"
domain = "google.com"
c = Whois::Client.new
a = c.query(domain)
puts a
change the name of your file - there is ambiguity in require 'whois' and ruby is requireing your file instead of a gem. when you do it line by line in irb ruby knows what you exactly want to require, so everything works.