Net::Ssh with Non Unix/Linux Host

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

ruby net-ssh calling bash script with interactive prompts

managed to fix this by using the channel function, here's the method I use now

def connect(host,command)

require 'rubygems'
require 'net/ssh'

user = LocalConfig::SSH_DETAILS[:user]
pass = LocalConfig::SSH_DETAILS[:pass

o = ""
Net::SSH.start(host, user, :password => pass, :paranoid => false, :auth_methods => ['password'], :timeout => 30 )do |ssh |
channel = ssh.open_channel do |ch|
ch.exec(command) do |ch2, success|
ch2.send_data "myUserName\n"
ch2.send_data "mPassWord\n"

ch.on_data do |ch2, data|
o += data
end
end
end
channel.wait
return o.to_s
end
end

ruby net-ssh login shell

Net-SSH is too low level to simply provide this up front (the way it is now, anyways). You can check out Net-SSH-Shell which builds upon Net-SSH to add login shell functionality: https://github.com/mitchellh/net-ssh-shell

The implementation is solid and works, however I found its not too useful since you can't specifically extract things like stderr or exit status because the commands run in a sub-shell, so you can only get stdout. The net-ssh-shell library uses some hacks to get the exit status.

I've needed a "login shell" for my own Ruby projects and to do this I've generally executed things directly into the shell using the following code:

def execute_in_shell!(commands, shell="bash")
channel = session.open_channel do |ch|
ch.exec("#{shell} -l") do |ch2, success|
# Set the terminal type
ch2.send_data "export TERM=vt100\n"

# Output each command as if they were entered on the command line
[commands].flatten.each do |command|
ch2.send_data "#{command}\n"
end

# Remember to exit or we'll hang!
ch2.send_data "exit\n"

# Configure to listen to ch2 data so you can grab stdout
end
end

# Wait for everything to complete
channel.wait
end

With this solution you still don't get exit status or stderr of commands run into the login shell, but at least the commands are executed in that context.

I hope this helps.

Unable to run perl script for ssh login to Windows server (using Net::SSH::Perl) from crontab

It looks like host key verification is failing preventing the authentication. There may be host keys stored in ~/.ssh/known_hosts in the user's(root) home directory try deleting the remote host key from "known_hosts" and then re-try.



Related Topics



Leave a reply



Submit