简体   繁体   English

Perl SFTP 真的很慢

[英]Perl SFTP really really slow

I'm using the perl Net:SSH2 library to do SFTP.我正在使用 perl Net:SSH2 库来执行 SFTP。 It works well enough but is super slow.它运行良好,但速度非常慢。 It starts off being 7x slower than the link speed (something I could handle) but pretty much as soon as it has transferred 2MB of data it drops to being 1000x slower.它开始时比链接速度慢 7 倍(我可以处理),但几乎一旦它传输了 2MB 数据,它就会下降到 1000 倍慢。 Here's a cut down version of my code.这是我的代码的简化版本。 I used this Linux command to create a 10M file of random data dd if=/dev/urandom of=10M.dat bs=1M count=10 .我使用这个 Linux 命令创建了一个 10M 的随机数据文件dd if=/dev/urandom of=10M.dat bs=1M count=10 For anyone who would like to run the code please enter details for server, user, password etc.对于任何想要运行代码的人,请输入服务器、用户、密码等的详细信息。

#!/usr/bin/perl

use strict;
use warnings;
use Fcntl ('O_WRONLY', 'O_CREAT');
use Net::SSH2;
use Time::HiRes qw(gettimeofday);

my $server = 'myserver';
my $username = 'myusername';
my $password = 'mypassword';
my $remoteFile = 'delme.dat';
my $localFile = '10M.dat';

my $ssh = Net::SSH2->new();
$ssh->connect($server) or die('Failed to connect to remote server for sftp');
$ssh->auth_password($username, $password);
$ssh->auth_ok() or die('Invalid username or password');
my $sftp = $ssh->sftp() or die('Ahhh!!');
open(my $localHandle, '<', $localFile) or die($!);
my $remoteHandle = $sftp->open($remoteFile, O_WRONLY | O_CREAT) or die($sftp->error());
my $tran = 0;
my $t = gettimeofday();
while(my $line = <$localHandle>)
{
  print $remoteHandle $line;
  $tran += length($line);
  my $newTime = gettimeofday();
  if($newTime - $t > 1)
  {
    print 'Speed is ' . ($tran / ($newTime - $t)) . "B/sec\n";
    $t = $newTime;
    $tran = 0;
  }
}
close($remoteHandle);
close($localHandle);

Support for SFTP in Net::SSH2/libssh2 is suboptimal. Net::SSH2/libssh2对 SFTP 的支持并不理想。

Use Net::SFTP::Foreign instead (with the Net::SSH2 backend Net::SFTP::Foreign::Backend::Net_SSH2 if you are on Windows).使用Net::SFTP::Foreign代替(如果您使用的是 Windows,则使用Net::SSH2 backend Net::SFTP::Foreign::Backend::Net_SSH2 )。

I hope you are facing the Session Re-Keying issue by OpenSSH as mentioned in the below link. 希望您遇到以下链接中提到的OpenSSH的Session Re-Keying问题。

http://crashingdaily.wordpress.com/2007/02/20/a-workaround-for-netsftp-hanging-on-large-transfers/ http://crashingdaily.wordpress.com/2007/02/20/a-workaround-for-netsftp-hanging-on-large-transfers/

The link has a fix for this problem as well. 该链接也有针对此问题的修复程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM