我没有很多红宝石,但这似乎有效!
先决条件,
sudo yum install rubygems
sudo gem install net-ssh net-sftp highline echoe
代码(带注释),
#!/usr/bin/env ruby
require 'rubygems'
require 'net/ssh'
require 'net/sftp'
require 'highline/import'
file = ARGV[ 0 ] # filename from command line
prod = file + "-new" # product filename (call it <file>-new)
rpath = "/tmp" # remote computer operating directory
rfile = "#{rpath}/#{file}" # remote filename
rprod = "#{rpath}/#{prod}" # remote product
cmd = "mv #{rfile} #{rprod}" # remote command, constructed
host = "-YOUR REMOTE HOST-"
user = "-YOUR REMOTE USERNAME-"
pass = ask("Password: ") { |q| q.echo = false } # password from stdin
Net::SSH.start(host, user, :password => pass) do |ssh|
ssh.sftp.connect do |sftp|
# upload local 'file' to remote 'rfile'
sftp.upload!(file, rfile)
# run remote command 'cmd' to produce 'rprod'
ssh.exec!(cmd)
# download remote 'rprod' to local 'prod'
sftp.download!(rprod, prod)
end
end
然后我可以这样运行,
dylan@home ~/tmp/ruby) ls
bar remotefoo.rb*
dylan@home ~/tmp/ruby) ./remotefoo.rb bar
Password:
dylan@home ~/tmp/ruby) ls
bar bar-new remotefoo.rb*