[Ssh-sftp-perl-users] 2 Errors with Net-SSH .08
Brought to you by:
dbrobins
From: J <mer...@ya...> - 2006-05-15 17:43:44
|
I have 2 errors i'm receiving using the newest version of Net-SSH. My script runs on a unix env and connects to a windows env, then downloads all the files in that directory then sleeps. Wakes up and repeat. 1. I can only download 99 files at a time. After that the rest of the files in a directory get an error "Unsuccessful getting file" and the error code is -31. I'd like to be able to download all the files instead of just 99. 2. At least once a day the script will not be able to connect and i'll get a -16 error. I tried having it sleep and then retry and every retry still cannot connect. The only way i've found to get around this is when it can't connect and it stops I restart it. Has anyone seen this before or can explain why it's not working? Code Start: #! /usr/local/bin/perl # use Net::SSH2; use strict; umask 0111; my $count; ### Define 'constants' ### my $CONT_FL = 1; my $STOP_FL = 0; my $MSG_FL = 2; my $stopped = 0; my $fatal_error = 0; # Infinite loop so the program doesn't stop unless killed or finds an error. while (1) { $count = 0; # Change local directory to specified destination directory my $local_dir = chdir("/unixdir"); if ($local_dir != 1) { werr_msg("Unable to change local destination directory.", $STOP_FL); } my @test = `rm -f /unixdir/*`; # Setup connection to the web server my $conn = Net::SSH2->new(); eval { $conn->connect("webserver"); }; werr_msg("Unable to connect to. Reason: $@", $STOP_FL) if $@; my $status = $conn->auth_publickey("username", "$ENV{'HOME'}/.ssh/id_rsa.pub", "$ENV{'HOME'}/.ssh/id_rsa"); if ($status) { my $sf = $conn->sftp(); my $directory = $sf->opendir("/temp"); if(!defined($directory)) { werr_msg("Unable to change to source directory.", $STOP_FL); } while (my $readObj = $directory->read) { my $fname = $readObj->{name}; if ($fname ne "." && $fname ne "..") { my $fileo = $sf->open("/temp/$fname"); if(defined($fileo)) { werr_msg("Start transfering file $fname from.", $MSG_FL); $count++; my $fbuf; open(DEST, ">$fname"); my $readSize = $fileo->read($fbuf, 6144); while($readSize != 0) { print DEST $fbuf; $readSize = $fileo->read($fbuf, 6144); } close(DEST); } else { # error in getting the file, skip to the next file werr_msg("Unsuccessful in getting file $fname.", $CONT_FL); } } } } else { print $conn->error . "\n"; werr_msg("Unable to connect to.", $STOP_FL); } $conn->disconnect(); werr_msg("Done transfering $count files from.", $MSG_FL); if ($fatal_error) { werr_msg("Fatal error, cannot continue processing", $STOP_FL); } werr_msg("Done transfering files, sleeping.", $MSG_FL); sleep(60); } exit(0); # ---------------------------------------------------------------------- # Function: werr_msg # Purpose: Writes error/warning messages # Author: L. McMillion, 03-09-99, SRA Corporation # Revised: Hector Caro, 07-01-00, SRA Corporation # Assumptions: Requires continuation or stop flag as argument # # ---------------------------------------------------------------------- sub werr_msg { my($msg, $continue) = @_; my $cur_time = localtime(time()); if ($continue == $CONT_FL) { print "$cur_time: Warning in ", __FILE__, ": ", $msg, "\n"; warn("$cur_time: Warning in ", __FILE__, ": ", $msg, "\n"); } elsif ($continue == $STOP_FL) { print ("\nError in ", __FILE__, ": ", $msg," Check logfile for details.\n\n"); die("$cur_time: Error in ", __FILE__, ": ", $msg, " ($!)\n"); } else { print (STDERR "$cur_time: ", $msg, "\n"); } } Code End |