Kumar C. Kumar C. - 5 months ago 16
Ruby Question

Sending a command via ssh with a publish ssh key, with no password

I can send a command via ssh to a server with login and password like this:

Net::SSH.start("1.2.3.4", "user_name", password: "1234") do |ssh|
res = ssh.exec!("ls")
end


But if I want to avoid providing a password and if I've added my public ssh key to that server, how can I get rid of the password? Should I just remove it from the function call or should I somehow let the server know that I want to be authenticated by a ssh public key?

Answer
Net::SSH.start("1.2.3.4", "user_name", keys: ['~/.ssh/id_rsa.pub']) do |ssh|
  res = ssh.exec!("ls")
end

If the above won’t work for you, do:

Net::SSH.start("1.2.3.4", "user_name", 
               keys: ['~/.ssh/id_rsa.pub'],
               # host_key: "ssh-rsa", # default value
               # encryption: "blowfish-cbc", # default value
               verbose: :debug
) do |ssh|
  res = ssh.exec!("ls")
end

to see what’s wrong.

Comments