From f451392f7ec4872f621d0686d5aeebcc04a7fff6 Mon Sep 17 00:00:00 2001 From: Marcel Date: Thu, 23 Apr 2020 18:20:25 +0200 Subject: [PATCH 1/4] Update scp.rb add scp limit --- lib/net/scp.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/net/scp.rb b/lib/net/scp.rb index fb7f53b..99fddda 100644 --- a/lib/net/scp.rb +++ b/lib/net/scp.rb @@ -334,6 +334,7 @@ def scp_command(mode, options) command << " -v" if options[:verbose] command << " -r" if options[:recursive] command << " -p" if options[:preserve] + command << " -l #{options[:limit]}" if options[:limit] command end From cd7d95cbe97c6bdcace1e2571d8eb9ef637828f3 Mon Sep 17 00:00:00 2001 From: Marcel Date: Thu, 23 Apr 2020 18:28:16 +0200 Subject: [PATCH 2/4] Update scp.rb debug code --- lib/net/scp.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/net/scp.rb b/lib/net/scp.rb index 99fddda..45e2f80 100644 --- a/lib/net/scp.rb +++ b/lib/net/scp.rb @@ -335,6 +335,7 @@ def scp_command(mode, options) command << " -r" if options[:recursive] command << " -p" if options[:preserve] command << " -l #{options[:limit]}" if options[:limit] + puts command command end @@ -351,7 +352,8 @@ def start_command(mode, local, remote, options={}, &callback) else command = "#{scp_command(mode, options)} #{shellescape remote}" end - + puts command + channel.exec(command) do |ch, success| if success channel[:local ] = local From 649f0648abb9e9f47f0e5c8e661b28725b623484 Mon Sep 17 00:00:00 2001 From: Marcel Date: Thu, 23 Apr 2020 18:37:18 +0200 Subject: [PATCH 3/4] Revert "Update scp.rb" This reverts commit cd7d95cbe97c6bdcace1e2571d8eb9ef637828f3. --- lib/net/scp.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/net/scp.rb b/lib/net/scp.rb index 45e2f80..99fddda 100644 --- a/lib/net/scp.rb +++ b/lib/net/scp.rb @@ -335,7 +335,6 @@ def scp_command(mode, options) command << " -r" if options[:recursive] command << " -p" if options[:preserve] command << " -l #{options[:limit]}" if options[:limit] - puts command command end @@ -352,8 +351,7 @@ def start_command(mode, local, remote, options={}, &callback) else command = "#{scp_command(mode, options)} #{shellescape remote}" end - puts command - + channel.exec(command) do |ch, success| if success channel[:local ] = local From 5df2b49bbb3f9839d825c4771f3b7796af920351 Mon Sep 17 00:00:00 2001 From: Marcel Date: Fri, 24 Apr 2020 10:09:38 +0200 Subject: [PATCH 4/4] Adding some documentation for limit --- README.rdoc | 7 ++++++- lib/net/scp.rb | 6 +++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/README.rdoc b/README.rdoc index 436c677..eeddc45 100644 --- a/README.rdoc +++ b/README.rdoc @@ -32,7 +32,7 @@ In a nutshell: Net::SCP.upload!("remote.host.com", "username", "/local/path", "/remote/path", :ssh => { :password => "password" }) - + # upload recursively Net::SCP.upload!("remote.host", "username", "/path/to/local", "/path/to/remote", :ssh => { :password => "foo" }, :recursive => true) @@ -42,6 +42,11 @@ In a nutshell: "/remote/path", "/local/path", :ssh => { :password => "password" }) + # download a file from a remote server with a speed limit (kbps) + Net::SCP.download!("remote.host.com", "username", + "/remote/path", "/local/path", + :limit => 400, :ssh => { :password => "password" }) + # download a file to an in-memory buffer data = Net::SCP::download!("remote.host.com", "username", "/remote/path") diff --git a/lib/net/scp.rb b/lib/net/scp.rb index 99fddda..4c90887 100644 --- a/lib/net/scp.rb +++ b/lib/net/scp.rb @@ -93,6 +93,8 @@ module Net # be sent to indicate the modification and access times of each file. # * "-r" -- recursive transfers should be allowed. Without this, it is an # error to upload or download a directory. + # * "-l" -- limit the speed of transfers. The value of :limit wil be use + # as the value in the -l command. # # After those flags, the name of the remote file/directory should be passed # as the sole non-switch argument to scp. @@ -265,6 +267,7 @@ def initialize(session) # * :chunk_size - the size of each "chunk" that should be sent. Defaults # to 2048. Changing this value may improve throughput at the expense # of decreasing interactivity. + # * :limit - Limits the speed of the transfer. Defaults no limit is set. # # This method will return immediately, returning the Net::SSH::Connection::Channel # object that will support the upload. To wait for the upload to finish, @@ -293,6 +296,7 @@ def upload!(local, remote, options={}, &progress) # * :preserve - the atime and mtime of the file should be preserved. # * :verbose - the process should result in verbose output on the server # end (useful for debugging). + # * :limit - limit the speed of the transfer. # # This method will return immediately, returning the Net::SSH::Connection::Channel # object that will support the download. To wait for the download to finish, @@ -326,7 +330,7 @@ def download!(remote, local=nil, options={}, &progress) # Constructs the scp command line needed to initiate and SCP session # for the given +mode+ (:upload or :download) and with the given options - # (:verbose, :recursive, :preserve). Returns the command-line as a + # (:verbose, :recursive, :preserve, :limit). Returns the command-line as a # string, ready to execute. def scp_command(mode, options) command = "scp "