|
Posted by Konstantin Schaefer on October 25, 2004, 2:39 am
Please log in for more thread options
Dear Group,
I'm coding on a Perl-based Deamon which should distribute commands
and files by request in a very large network over SSH and SFTP.
For this, Net::SSH::Perl and Net::SFTP are used. The problem I
have is, that when I receive a data from my client, and send
it to SSH::Perl then the execution hangs (SSH::Perl) or disconnects
(SFTP).
The idea is to send large files via SFTP, by reading them from a
network socket in parts of ~64kb and send them over SFTP.
If I try this SFTP disconnects after a random number of packages.
So I tried it with SSH and called 'cat - >' on the remote server
with the file data on STDIN. SSH hangs if I try to send packages
larger than 10kb over stdin. And to call a 'cmd' for every 10kb
is horrible slow.
As I tried to do the following:
my $exp = "$ssh->cmd('/bin/cat - >', '$source');"
eval $exp;
Everything works... So I do something very, very wrong.
For me this sounds like the same problem. Maybe buffering or
something inside my code (?). I found some messages about this
but I could not find any bugs about this in my code. Especially
the received data came in Base64, is decoded correctly and then send
over the SSH connection.
Than I tried to load the whole data into the memory and then send
it to SFTP. This works perfectly.
Somebody an Idea?
Many thanks,
Konstantin
#### Code for SSH copy function ####
sub stupid_ssh_upload {
my $self = shift;
my $target_path = shift;
my $data = shift;
return undef unless($target_path);
return undef unless($data);
my $create_command = "/bin/cat - >" . $target_path;
my $append_command = "/bin/cat - >>" . $target_path;
my $remove_command = "/bin/rm -f " . $target_path;
my $offset = 0;
my $length = length($data);
my $packet_size = 1024 * 10;
#Remove existing file first
my ($out, $err, $ret)
= $self->ssh_conn->cmd($remove_command);
die "SSHStupidUploadCannontRemove" if($ret);
#Append now
PACKET: while(1) {
my $packet = substr($data, $offset, $packet_size);
($out, $err, $ret) =
$self->ssh_conn->cmd($append_command, $packet);
die "SSHStupidUploadAppendFailure" if($ret);
last PACKET
if(length($packet) <= $packet_size
and $offset + $packet_size >= $length);
$offset += $packet_size;
}
return $self;
}
#### Code for the SFTP copy functions ###
This is called in a loop:
$handle = $o->sftp_partial_upload_init("/somewhere");
while(something) {
$o->sftp_partial_upload_send(decode_base64($data), $handle);
}
$o->sftp_partial_upload_finish($handle);
sub sftp_partial_upload_init {
my $self = shift;
my $target_path = shift;
return undef unless($self->sftp_conn);
my $handle = $self->sftp_conn->do_open($target_path,
SSH2_FXF_WRITE
| SSH2_FXF_CREAT
| SSH2_FXF_TRUNC);
die "SFTPCannotOpenFileW: $target_path" unless $handle;
return $handle;
}
sub sftp_partial_upload_send {
my $self = shift;
my $data = shift;
my $handle = shift;
return undef unless($data and $handle);
my $offset = 0;
my $packet_size = 1024 * 8;
my $length = length($data);
PACKET: while(1) {
my $packet = substr($data, $offset, $packet_size);
my $status;
$status = $self->sftp_conn->do_write($handle, $offset, $packet);
die "SFTPWriteFailure: $statusn"
unless($status == SSH2_FX_OK);
last PACKET
if(length($packet) <= $packet_size
and $offset + $packet_size >= $length);
$offset += length($packet);
}
return $self;
}
sub sftp_partial_upload_finish {
my $self = shift;
my $handle = shift;
return undef unless($handle);
die "SFTPCannotCloseFileW"
unless($self->sftp_conn->do_close($handle) == SSH2_FX_OK);
return $self;
}
####
KS_FIND_ME
|
| Similar Threads | Posted | | fedora core 5 + install net::ssh::perl = hangs | June 25, 2007, 6:33 am |
| Perl programmer needed | June 22, 2006, 2:12 am |
| advice needed on using proper perl graphics module(s) | November 4, 2004, 1:02 pm |
| Help with Net::SFTP when sftp on unix works. | September 21, 2004, 11:14 am |
| cpan Net::FTP hangs | February 26, 2005, 8:22 pm |
| GraphViz in Windows hangs | May 1, 2006, 4:06 pm |
| LWP::UserAgent hangs up infinitively | February 26, 2007, 9:05 am |
| Net::FTP help needed | January 15, 2007, 9:58 pm |
| Help needed with XML::Smart | September 23, 2004, 9:41 am |
| Help needed with MakeMaker | August 15, 2005, 2:37 pm |
|