Using Ruby and net-ssh, how do I authenticate using the key_data parameter using Net :: SSH.start?

I read the net-ssh documentation and I'm still perplexed. I can perform authentication manually (using ssh -i ...), as well as placing the key in a file and using the: keys parameter. However, I do not want to use the: keys parameter, I want to use the: key_data parameter. Can someone give a working example? For some reason, directly nesting a string in: key_data does not work, and it gives an error: "Neither the PUB key nor the PRIV key :: inested asn1 error". Of course, I searched this on Google, and it basically tells me that the key was in PEM format. And, of course, the way it is. Any ideas? If necessary, I can provide more detailed information ...

+5
authentication ruby net-ssh
source share
2 answers

I see this question quite old, but I'm still going to answer this question just in case, since I had the same problem and I just solved it.

In the following code, note that the line containing the RSA key is not indented anywhere. The second line of the key has no leading place in it. TextMate put this when I inserted the key. I deleted it and it worked like a charm.

#!/usr/bin/env ruby require 'rubygems' require 'net/ssh' HOST = '172.20.0.31' USER = 'root' KEYS = [ "-----BEGIN RSA PRIVATE KEY----- MIIEogIBAAKCAQEAqccvUza8FCinI4X8HSiXwIqQN6TGvcNBJnjPqGJxlstq1IfU kFa3S9eJl+CBkyjfvJ5ggdLN0S2EuGWwc/bdE3LKOWX8F15tFP0= -----END RSA PRIVATE KEY-----" ] Net::SSH.start( HOST, USER, :key_data => KEYS, :keys_only => TRUE) do|ssh| result = ssh.exec!('ls') puts result end 
+9
source share

I am adding a little more information that I discovered after digging out the library ...

Starting from 2.9.2, if you intend to use only the key provided in key_data, you must also specify an empty key set before loading key_data or loading some standard keys.

In my case, one of the identifier files that he was trying to download was password protected, so he asked me to use my passphrase, although my intention was not to use this identification file at all.

Using the example above in 2.9.2, you can get the same effect by doing something like this:

 #!/usr/bin/env ruby require 'rubygems' require 'net/ssh' HOST = '172.20.0.31' USER = 'root' KEYS = [ "-----BEGIN RSA PRIVATE KEY----- MIIEogIBAAKCAQEAqccvUza8FCinI4X8HSiXwIqQN6TGvcNBJnjPqGJxlstq1IfU kFa3S9eJl+CBkyjfvJ5ggdLN0S2EuGWwc/bdE3LKOWX8F15tFP0= -----END RSA PRIVATE KEY-----" ] Net::SSH.start( HOST, USER, :keys => [], :key_data => KEYS, :keys_only => TRUE) do|ssh| result = ssh.exec!('ls') puts result end 
+6
source share

All Articles