Net::SSH with non unix/linux host?
I assume it works fine when you login in the shell manually.
To understand what is the difference when you connect through net/ssh collect output of env
command in both cases and compare.
That most probably you'll see a difference that will lead you to a solution or at least will give you dirty trick.
UPDATE. (Not working)
Net::SSH.start('10.204.121.192', 'password', :password => "password") do |ssh|
ssh.open_channel do |channel|
channel.on_data do |ch, data|
puts "got data: #{data.inspect}"
end
channel.send_data("INH-MSG-ALL;\n")
end
end
UPDATE2. (Working)
Net::SSH.start('10.204.121.192', 'password', :password => "password") do |ssh|
ssh.open_channel do |channel|
channel.send_channel_request "shell"
channel.on_data do |ch, data|
puts "got data: #{data.inspect}"
end
channel.send_data("INH-MSG-ALL;\n")
end
end
How to SSH interactive Session
Something like this?
Net::SSH.start('10.0.0.6', 'not_root', :password => "test") do |ssh|
ssh.open_channel do |channel|
channel.on_request "exit-status" do |channel, data|
$exit_status = data.read_long
end
channel.exec("passwd") do |channel, success|
if success
channel.on_data do |channel, data|
# Don't really need this callback actually
puts "got data: #{data.inspect}"
end
# You don't need this line if you're root
channel.send_data("oldpass\n")
channel.send_data("newpass\n")
channel.send_data("newpass\n")
else
puts "FAILED"
end
end
channel.wait
puts "SUCCESS" if $exit_status == 0
end
end
This one is dirty, but working for both upon premature on-expiration prompt and upon passwd
issuing:
Net::SSH.start('localhost', 'not_root', :password => "test") do |ssh|
ssh.open_channel do |channel|
channel.on_request "exit-status" do |channel, data|
$exit_status = data.read_long
end
channel.on_data do |channel, data|
puts data.inspect
if data.inspect.include? "current"
channel.send_data("oldpass\n");
elsif data.inspect.include? "new"
channel.send_data("newpass\n");
end
end
channel.request_pty
# This will just safely fail if we have already a password prompt on
channel.exec("passwd");
channel.wait
# Will reflect a valid status in both cases
puts $exit_status
end
end
How can i execute 2 or more commands in the same ssh session?
see if there's something analogous to the file(utils?) cd block syntax, otherwise just run the command in the same subshell, e.g. ssh.exec "cd /var/example/engines/; pwd" ?
How to execute instructors in order with ruby net ssh channel?
Use sync shell service for it, do something like:
Net::SSH.start( 'localhost' ) do |session|
shell = session.shell.sync
shell.touch 'a.file'
shell.mv 'a.file', 'b.file'
shell.send_command("rm", 'b.file')
shell.exit
end
or just organize the commands on-a-line:
work session, "touch a.file; mv a.file b.file; rm b.file"
Net::SSH::Multi using the session.exec, how do you get the output straight away? Ruby
Adding session.loop after session.exec allows the program to wait for the output.
Such as:
session.exec(command)do |ch, stream, data|
puts "[#{ch[:host]} : #{stream}] #{data}"
end
session.loop
# Or session.wait also does the same job.
What is the cleanest way to ssh and run multiple commands in Bash?
How about a Bash Here Document:
ssh otherhost << EOF
ls some_folder;
./someaction.sh 'some params'
pwd
./some_other_action 'other params'
EOF
To avoid the problems mentioned by @Globalz in the comments, you may be able to (depending what you're doing on the remote site) get away with replacing the first line with
ssh otherhost /bin/bash << EOF
Note that you can do variable substitution in the Here document, but you may have to deal with quoting issues. For instance, if you quote the "limit string" (ie. EOF
in the above), then you can't do variable substitutions. But without quoting the limit string, variables are substituted. For example, if you have defined $NAME
above in your shell script, you could do
ssh otherhost /bin/bash << EOF
touch "/tmp/${NAME}"
EOF
and it would create a file on the destination otherhost
with the name of whatever you'd assigned to $NAME
. Other rules about shell script quoting also apply, but are too complicated to go into here.
Related Topics
Rails Active Admin CSS Conflicting with Twitter Bootstrap CSS
Ruby Ssl Error - Sslv3 Alert Unexpected Message
Functional Code Examples in Ruby
How to Push to Faye Server from Rails Controller
Escape Single Quote in Xpath with Nokogiri
Why Can't I Use an Integer as a Key Using the New Ruby 1.9.2 Hash Syntax
How Does Require Rubygems Help Find Rubygem Files
What Does &: Mean in Ruby, Is It a Block Mixed with a Symbol
How to Improve Jruby Load Time
Error: While Executing Gem ... (Zlib::Gzipfile::Error) Not in Gzip Format
Travis Reports Odd Message of Corrupted Gemfile.Lock