Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using SCP/SSH in Ruby with a pem file in an Amazon EC2 instance

Trying to transfer or run commands remotely from an Amazon EC2(Ubuntu) instance with a Ruby script. I am not able to figure out from the ruby doc for ssh and scp how the .pem file can be passed for authentication

# download a file from a remote server
Net::SCP.download!("remote.host.com", "username",
"/remote/path", "/local/path",
:password => password)

I have also tried using command line but the issue here is the host is dynamic and I would have to authenticate 'yes' everytime

`/usr/bin/scp -i keyfile.pem -r [email protected]:/remote/path /local/path`

The authenticity of host 'some.random.ip (some.random.ip)' can't be established.
ECDSA key fingerprint is some:random:fingerprint.
Are you sure you want to continue connecting (yes/no)? yes

Is there any way other than by not disabling SSH host key checking with the command line code. Or is there an option in the net-scp or the net-ssh gem for ruby?

like image 467
naveed Avatar asked Dec 26 '22 20:12

naveed


1 Answers

Ruby solution which I did not find in any of their docs

Net::SSH.start( hostname, username, :keys => "/path/to/keyfile.pem" ) do|ssh|
    #process
end
like image 151
naveed Avatar answered Dec 28 '22 09:12

naveed