net-ssh

Updated at: February 23, 2017

net-ssh @github

install gem net-ssh

  • run gem install net-ssh directly
  • or download manually, then run gem install [downloaded gem file] if you are in limited network.

sample try

require 'net/ssh'

#ssh by password

Net::SSH.start('host', 'username', :password => "passwd") do |ssh|
    output = ssh.exec!("hostname")
    puts output
end

#ssh via identical file
Net::SSH.start('host', 'username', :keys=> "primary key file") do |ssh|
    output = ssh.exec!("hostname")
    puts output
end

#forward port
Net::SSH.start('gateway host', 'gateway user', :password => "gateway passwd") do |ssh|
    begin
        ssh.forward.local(local port,"remote host",reomte port)
        ssh.loop {true}
        rescue
        puts 'ssh session loop ended'
    end
end