Created
March 24, 2022 12:58
-
-
Save saggineumann/2271caef98fc12fe314afe65c38ac47d to your computer and use it in GitHub Desktop.
sftp.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'net/sftp' | |
require 'uri' | |
class SFTPClient | |
def initialize(host, user, password) | |
@host = host | |
@user = user | |
@password = password | |
end | |
def connect | |
sftp_client.connect! | |
rescue Net::SSH::RuntimeError | |
puts "Failed to connect to #{@host}" | |
end | |
def disconnect | |
sftp_client.close_channel | |
ssh_session.close | |
end | |
def sftp_client | |
@sftp_client ||= Net::SFTP::Session.new(ssh_session) | |
end | |
private | |
def ssh_session | |
@ssh_session ||= Net::SSH.start(@host, @user, @password) | |
end | |
end | |
sftptogo_url = ENV['SFTPTOGO_URL'] | |
begin | |
uri = URI.parse(sftptogo_url) | |
rescue URI::InvalidURIError | |
puts 'Bad SFTPTOGO_URL' | |
end | |
sftp = SFTPClient.new(uri.host, uri.user, password: uri.password) | |
sftp.connect | |
# disconnect | |
sftp.disconnect |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def download_file(remote_path, local_path) | |
@sftp_client.download!(remote_path, local_path) | |
puts "Downloaded #{remote_path}" | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def list_files(remote_path) | |
@sftp_client.dir.foreach(remote_path) do |entry| | |
puts entry.longname | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'net/sftp' | |
require 'uri' | |
class SFTPClient | |
def initialize(host, user, password) | |
@host = host | |
@user = user | |
@password = password | |
end | |
def connect | |
sftp_client.connect! | |
rescue Net::SSH::RuntimeError | |
puts "Failed to connect to #{@host}" | |
end | |
def disconnect | |
sftp_client.close_channel | |
ssh_session.close | |
end | |
def upload_file(local_path, remote_path) | |
@sftp_client.upload!(local_path, remote_path) | |
puts "Uploaded #{local_path}" | |
end | |
def download_file(remote_path, local_path) | |
@sftp_client.download!(remote_path, local_path) | |
puts "Downloaded #{remote_path}" | |
end | |
def list_files(remote_path) | |
@sftp_client.dir.foreach(remote_path) do |entry| | |
puts entry.longname | |
end | |
end | |
def sftp_client | |
@sftp_client ||= Net::SFTP::Session.new(ssh_session) | |
end | |
private | |
def ssh_session | |
@ssh_session ||= Net::SSH.start(@host, @user, @password) | |
end | |
end | |
sftptogo_url = ENV['SFTPTOGO_URL'] | |
begin | |
uri = URI.parse(sftptogo_url) | |
rescue URI::InvalidURIError | |
puts 'Bad SFTPTOGO_URL' | |
end | |
sftp = SFTPClient.new(uri.host, uri.user, password: uri.password) | |
sftp.connect | |
# list files in directory | |
sftp.list_files('/path/to/remote') | |
# upload files | |
sftp.upload_file('/path/to/local', '/path/to/remote') | |
# download files | |
sftp.download_file('/path/to/remote', '/path/to/local') | |
# disconnect | |
sftp.disconnect |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def upload_file(local_path, remote_path) | |
@sftp_client.upload!(local_path, remote_path) | |
puts "Uploaded #{local_path}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment