17
Help us understand the problem. What are the problem?

More than 5 years have passed since last update.

RubyでSFTP

net/sftpを用いてsftpを利用する方法を簡単にまとめる。

接続

net/sshに依存している

パスワードを用いて接続する方法


option = {
  password: 'password',
  port: 22,
}

Net::SFTP.start('host', 'username', option) do |sftp|
end

秘密鍵を用いる方法

option = {
  keys: '/path/to/your/identity_key',
  passphrase: 'your-identity-key-pass-phrase',
  port: 22,
}

Net::SFTP.start('host', 'username', option) do |sftp|
end

ファイルのput/get

put


Net::SFTP.start('host', 'username', option) do |sftp|
  sftp.upload!('/path/to/local_file', '/path/to/remote_file')
end

get


Net::SFTP.start('host', 'username', option) do |sftp|
  sftp.download!("/path/to/remote_file", "/path/to/local_file")
end

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Sign upLogin
17
Help us understand the problem. What are the problem?