托特蘭回答了我提出的問題,但亞當的方法就是我最終使用的方法。這是我完成的腳本,其中確實顯示正在下載的字節的運行計數,並且還完成了一個百分比。
require 'rubygems'
require 'net/scp'
puts "Fetching file"
# Establish the SSH session
ssh = Net::SSH.start("IP Address", "username on server", :password => "user's password on server", :port => 12345)
# Use that session to generate an SCP object
scp = ssh.scp
# Download the file and run the code block each time a new chuck of data is received
scp.download!("path/to/file/on/server/fileName", "/Users/me/Desktop/") do |ch, name, received, total|
# Calculate percentage complete and format as a two-digit percentage
percentage = format('%.2f', received.to_f/total.to_f * 100) + '%'
# Print on top of (replace) the same line in the terminal
# - Pad with spaces to make sure nothing remains from the previous output
# - Add a carriage return without a line feed so the line doesn't move down
print "Saving to #{name}: Received #{received} of #{total} bytes" + " (#{percentage}) \r"
# Print the output immediately - don't wait until the buffer fills up
STDOUT.flush
end
puts "Fetch complete!"
你這裏有兩個正交的問題,因爲SCP默認情況下只輸出到終端,你需要* SCP -v * – tokland 2011-03-04 14:09:17
@tokland - 使其到達輸出調試消息,但不是說我會看到傳輸進度如果我只是單獨跑scp。我認爲這些不能達到標準輸出,並且我沒有看到scp將它們發送到那裏的選項。 – 2011-03-04 14:39:36
顯然scp發送進度信息到「交互式終端」?不知道如何捕獲... – 2011-03-04 14:45:27