Thank you for everyones remarks. Further investigation found that the Telnet module needs to read in blocks when trying to populate a local variable...Should have researched this a little further before posting. Thanks again.
## Make sure prompt won't match anything in send data.
$prompt = "_funkyPrompt_";
$host->prompt("/$prompt\$/");
$host->cmd("set prompt = '$prompt'");
## Get size of file.
($line) = $host->cmd("/bin/ls -l $filename");
($size_bsd, $size_sysv) = (split ' ', $line)[3,4];
if ($size_sysv =~ /^\d+$/) {
$size = $size_sysv;
}
elsif ($size_bsd =~ /^\d+$/) {
$size = $size_bsd;
}
else {
die "$filename: no such file on $hostname";
}
## Start sending the file.
binmode STDOUT;
$host->binmode(1);
$host->print("/bin/sh -c 'stty raw; cat $filename'");
$host->getline; # discard echoed back line
## Read file a block at a time.
$num_read = 0;
$prevblock = "";
$start_time = time;
while (($block = $host->get) and $block !~ /$prompt$/o)) {
if (length $block >= length $prompt) {
print $prevblock;
$num_read += length $prevblock;
$prevblock = $block;
}
else {
$prevblock .= $block;
}
}
$host->close;
## Print last block without trailing prompt.
$prevblock .= $block;
$prevblock =~ s/$prompt$//;
print $prevblock;
$num_read += length $prevblock;
die "error: expected size $size, received size $num_read\n"
unless $num_read == $size;
## Print totals.
$total_time = (time - $start_time) || 1;
$k_per_sec = ($size / 1024) / $total_time;
$k_per_sec = sprintf "%3.1f", $k_per_sec;
warn("$num_read bytes received in $total_time seconds ",
"($k_per_sec Kbytes/s)\n");
Edited by planetscape - added code tags
|