Key based authenication with net-sftp in Ruby

后端 未结 2 1364
温柔的废话
温柔的废话 2021-01-05 16:31

I want to be able to use SFTP to login into a number of servers and download certain files to help debug issues as and when they arise. While we could use a client, we wante

2条回答
  •  长发绾君心
    2021-01-05 16:49

    If you want to directly specify the key (or other SSH options) you can first open a Net::SSH connection, and then do SFTP operations from there.

    Net::SSH.start("localhost", "user", keys: ['keys/my_key']) do |ssh|
      ssh.sftp.upload!("/local/file.tgz", "/remote/file.tgz")
      ssh.exec! "cd /some/path && tar xf /remote/file.tgz && rm /remote/file.tgz"
    end
    

    This also works for Net::SCP

    Net::SSH.start('localhost', 'user', keys: ['keys/my_key'] ) do |ssh|
      ssh.scp.download("/local/file.txt", "/remote/file.txt")
    end
    

提交回复
热议问题