Created
September 22, 2011 11:39
-
-
Save 46bit/1234588 to your computer and use it in GitHub Desktop.
Handy scp uploading helper
This file contains hidden or 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
#!/usr/bin/env ruby | |
# This is a simple helper for uploading files to a server | |
# By default it randomises the filename (but preserves the extension) | |
# Supply -p to maintain original filename | |
require 'optparse' | |
require 'digest/md5' | |
require 'net/scp' | |
options = { } | |
optparse = OptionParser.new do |opts| | |
opts.banner = "Usage: r_upload [-p|--preserve] file" | |
options[:preserve] = false | |
opts.on('-p', '--preserve', 'Preserve current filename') do | |
options[:preserve] = true | |
end | |
opts.on('-h', '--help', 'Display this screen') do | |
puts opts | |
exit | |
end | |
end | |
optparse.parse! | |
file = ARGV[0] | |
name = file.split('/').last | |
if (file == nil) | |
puts "No file provided as argument." | |
exit | |
end | |
if (!options[:preserve]) | |
parts = name.split('.') | |
ext = (parts.length > 1) ? "." + parts.last : "" | |
name = Digest::MD5.hexdigest(name + String(rand())) + ext | |
end | |
puts "Uploading " + file + " as " + name + "." | |
Net::SCP.upload!('server', 'user', file, '/path/to/destination/' + name) | |
puts "Uploaded to http://server/" + name |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment