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

前端 未结 2 1170
时光取名叫无心
时光取名叫无心 2020-12-30 13:47

I\'ve read the net-ssh documentation, and I am still perplexed. I can authenticate manually (using ssh -i ...), and also by placing the key in a file and using the :keys pa

2条回答
  •  时光说笑
    2020-12-30 14:09

    I see this question in pretty old but I am going to throw the answer to you anyway just in case as I had the same issue and I just solved it.

    In the following code note that the string containing the RSA key is not indented at all anywhere. The second line of the key does not have any leading space in it. TextMate put this there when I pasted the key in. I removed 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
    

提交回复
热议问题