Remote server doesn't accept password, ruby script 'net/ssh' - ruby

I connect using ruby to the remote, pass the command, but channel.send_data ("passwd\n") dont work , I cant catch in than the reason. The password doesn't pass.
require 'rubygems'
require 'net/ssh'
...
Net::SSH.start(#hostname, #username, :password => #password) do |session|
stdout = ""
session.exec!(#cmd) do |channel, stream, data|
if data =~ /Password/
channel.send_data("passw\n")
end
stdout << data
end
puts stdout
session.close
end

Related

Error using gem net/ssh <= Net::SSH::AuthenticationFailed

While using the gem net/ssh I'm getting the error:
/usr/local/lib/ruby/gems/1.9.1/gems/net-ssh-2.0.23/lib/net/ssh.rb:192:in
`start': Net::SSH::AuthenticationFailed (Net::SSH::AuthenticationFailed)
I don't really understand what's going on..? I've done my research and discovered this but it doesn't really answer my question..
Is there a specific reason why the authentication is failing? All I'm doing is sshing to different servers, is there something specific I need to change?
Source:
require 'rubygems'
require 'net/ssh'
require 'etc'
print "Enter password: "
system "stty -echo"
#password = gets.chomp
system "stty echo"
def logged_in(server)
cmd = `who`.gsub(/[ \t].*/,"").gsub(/\A.*\n/,'')
check = Net::SSH.start(#host, #username, :password => #password) do |ssh|
ssh.exec!(cmd)
end
end
#host = %w(server_names_here) do |server|
logged_in(server)
end
#username = Etc.getlogin
I thought it might be the wrong password so I tried entering the password with the echo "on" and I am entering the correct password, I also thought maybe it's not pulling my username so I used: #username = 'my_username' I am still receiving the same error
Edit:
Found the problem, it had to do with where the #username was placed
The problem had to do with where #username was placed.
require 'rubygems'
require 'net/ssh'
require 'etc'
print "Enter password: "
system "stty -echo"
#password = gets.chomp
system "stty echo"
#username = Etc.getlogin #<= YAY!
def logged_in(server)
cmd = `who`.gsub(/[ \t].*/,"").gsub(/\A.*\n/,'')
check = Net::SSH.start(#host, #username, :password => #password) do |ssh|
ssh.exec!(cmd)
end
end
#host = %w(server_names_here) do |server|
logged_in(server)
end

Getting output from Ruby Net::SSH sessions

I've got a larger script that basically uses the same type of code below. The script runs and functions but I don't get output to the screen. How do I get the output of the script that is being executed remotely to show up on the screen I'm running the ruby script from?
#!/usr/bin/ruby
#
require 'rubygems'
require 'net/ssh'
require 'pty'
if ENV['USER'] == 'root'
raise "You can't run this as root"
end
Net::SSH.start(server01, testuser) 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|
data
end
channel.request_pty do |channel, data|
channel.exec("sudo -s")
channel.send_data("/tmp/scripts/test.sh\n")
channel.send_data("exit\n")
end
end
end
puts "DONE"
change
channel.on_data do |channel, data|
data
end
to
channel.on_data do |channel, data|
puts data
end
the data mentioned is the response from the server
and add
channel.send_channel_request 'shell' do |ch, success|
if success
puts 'user shell started successfully'
else
puts 'could not start user shell'
end
end

AuthenticationFailed net-ssh ruby

When I'm trying to Net::SSH.start to my debian ssh server and transfer a files, every time I've a very strange error message - `start': Net::SSH::AuthenticationFailed, but all the authentication data are correct, I don't know what a problem is. Does anyone faced same problem?
The code was written on ruby and net/ssh module are in use, here is a code:
require 'rubygems'
require 'net/ssh'
def copy_file(session, source_path, destination_path=nil)
destination_path ||= source_path
cmd = %{cat > "#{destination_path.gsub('"', '\"')}"}
session.process.popen3(cmd) do |i, o, e|
puts "Copying #{source_path} to #{destination_path}... "
open(source_path) { |f| i.write(f.read) }
puts 'Done.'
end
end
Net::SSH.start("192.168.112.129",
:username=>'username',
:password=>'password') do |session|
copy_file(session, 'D:/test/1.txt')
copy_file(session, '/home/timur/Documents/new_file.rb"')
end
There is no :username option in net/ssh 2.6, you can set it like parameter:
Net::SSH.start('192.168.112.129', 'username', password: 'password') do |ssh|
foo
end

How do I run Net::SSH and AMQP in the same EventMachine reactor?

Some background: Gerrit exposes an event stream through SSH. It's a cute trick, but I need to convert those events into AMQP messages. I've tried to do this with ruby-amqp and Net::SSH but, well, it doesn't seem as if the AMQP sub-component is even being run at all.
I'm fairly new to EventMachine. Can someone point out what I am doing incorrectly? The answer to "Multiple servers in a single EventMachine reactor) didn't seem applicable. The program, also available in a gist for easier access, is:
#!/usr/bin/env ruby
require 'rubygems'
require 'optparse'
require 'net/ssh'
require 'json'
require 'yaml'
require 'amqp'
require 'logger'
trap(:INT) { puts; exit }
options = {
:logs => 'kili.log',
:amqp => {
:host => 'localhost',
:port => '5672',
},
:ssh => {
:host => 'localhost',
:port => '22',
:user => 'nobody',
:keys => '~/.ssh/id_rsa',
}
}
optparse = OptionParser.new do|opts|
opts.banner = "Usage: kili [options]"
opts.on( '--amqp_host HOST', 'The AMQP host kili will connect to.') do |a|
options[:amqp][:host] = a
end
opts.on( '--amqp_port PORT', 'The port for the AMQP host.') do |ap|
options[:amqp][:port] = ap
end
opts.on( '--ssh_host HOST', 'The SSH host kili will connect to.') do |s|
options[:ssh][:host] = s
end
opts.on( '--ssh_port PORT', 'The SSH port kili will connect on.') do |sp|
options[:ssh][:port] = sp
end
opts.on( '--ssh_keys KEYS', 'Comma delimeted SSH keys for user.') do |sk|
options[:ssh][:keys] = sk
end
opts.on( '--ssh_user USER', 'SSH user for host.') do |su|
options[:ssh][:user] = su
end
opts.on( '-l', '--log LOG', 'The log location of Kili') do |log|
options[:logs] = log
end
opts.on( '-h', '--help', 'Display this screen' ) do
puts opts
exit
end
end
optparse.parse!
log = Logger.new(options[:logs])
log.level = Logger::INFO
amqp = options[:amqp]
sshd = options[:ssh]
queue= EM::Queue.new
EventMachine.run do
AMQP.connect(:host => amqp[:host], :port => amqp[:port]) do |connection|
log.info "Connected to AMQP at #{amqp[:host]}:#{amqp[:port]}"
channel = AMQP::Channel.new(connection)
exchange = channel.topic("traut", :auto_delete => true)
queue.pop do |msg|
log.info("Pulled #{msg} out of queue.")
exchange.publish(msg[:data], :routing_key => msg[:route]) do
log.info("On route #{msg[:route]} published:\n#{msg[:data]}")
end
end
end
Net::SSH.start(sshd[:host], sshd[:user],
:port => sshd[:port], :keys => sshd[:keys].split(',')) do |ssh|
log.info "SSH connection to #{sshd[:host]}:#{sshd[:port]} as #{sshd[:user]} made."
channel = ssh.open_channel do |ch|
ch.exec "gerrit stream-events" do |ch, success|
abort "could not stream gerrit events" unless success
# "on_data" is called when the process writes something to
# stdout
ch.on_data do |c, data|
json = JSON.parse(data)
if json['type'] == 'change-merged'
project = json['change']['project']
route = "com.carepilot.event.code.review.#{project}"
msg = {:data => data, :route => route}
queue.push(msg)
log.info("Pushed #{msg} into queue.")
else
log.info("Ignoring event of type #{json['type']}")
end
end
# "on_extended_data" is called when the process writes
# something to stderr
ch.on_extended_data do |c, type, data|
log.error(data)
end
ch.on_close { log.info('Connection closed') }
end
end
end
end
Net::SSH is not asynchronous, so your EventMachine.run() is never reaching the end of the block, thus never resuming the reactor thread. This causes the AMQP code to never start. I would suggest running your SSH code within another thread.
If you go back to EventMachine, give em-ssh https://github.com/simulacre/em-ssh a try.

ruby shoes ssh connection

I am trying my hands on shoes but got stuck. I am trying to connect to a remote computer using ssh and issue a command, got it working in cli but it is a no go for me to get it working on shoes.
This might be a simple thing but new as I am I can't get past it.
Here is what my code looks like atm
Shoes.setup do
gem 'net-ssh'
end
require "rubygems"
require "net/ssh"
Shoes.app do
button "Connect" do
append Net::SSH.start( '192.168.100.127', 'fox', :password => "xxxxxx" ) do
|ssh_connection|
ssh_connection.open_channel do |channel|
channel.on_data do |ch, data|
puts data
channel.exec "ls -la" do |ch, success|
para success
if success then
alert "uploaded"
else
alert "Fail"
end
end
end
end
end
end
end
Your code is trying to receive data first which is not the case usually. Remove the on_data:
Shoes.app do
button "Connect" do
append Net::SSH.start( '192.168.100.127', 'fox', :password => "xxxxxx" ) do |ssh_connection|
ssh_connection.open_channel do |channel|
channel.exec "ls -la" do |ch, success|
para success
if success then
alert "uploaded"
else
alert "Fail"
end
end
end
end
end
end

Resources