Speed up development with full-stack environments for every branch.

Learn More

NET SFTP Uploader w/ Progress Monitoring [Ruby and IO]

60 Runs 734 Views 125 Copies

Taken from Net::SFTP doc. SFTP uploader with progress monitoring. Added interface for easy setup. Works best with sanitize file if you want a tad more security.

Saved

Saved

arledgemike 8

arledgemike
published 3 years ago

#MUST DOWNLOAD NET-SFTP GEM TO RUN
require 'net/sftp'

class MeatShip 
  
  def initialize
    puts "What's host?"
    @host = gets.chomp
    
    puts "What's the username?"
    @user = gets.chomp
    
    puts "whats the password?"
    @pass = gets.chomp
    
    @connect = Net::SSH.start @host, @user, :password => @pass
     
    puts "what file do you want to uploaad?"
    @file = gets.chomp
    
    get_going @file

end

def get_going(file)
  @file = file
@connect.sftp.upload! @file do | event, uploader, *args |
  
  case event
     when :open then
     puts "starting upload: #{args[0].local} -> #{args[0].remote} (#{args[0].size} :bytes)"
   when :put then
    puts "writing #{args[2].length} bytes to #{args[0].remote} starting at #{args[1]}"
  when :close then 
   puts "finished writing #{args[0].remote}"
   when :mkidr then
   puts "making directory #{args[0]}"
  when :finish then
   puts "all done!"
   remove_file @file
  end
 
end
end


def remove_file(file)
  puts "Erase this file now?"
  @answer = gets.chomp
  @file = file
  if @answer
@connect.sftp.remove! @file do |response, event, *args |
   puts "#{response}"  

  end
end
end

end


@m = MeatShip.new
Please login/signup to get access to the terminal.

Your session has timed out.

Dismiss (the page may not function properly).