I am trying to duplicate a PERL script that uses Net::FTP to connect to a non-secure
server that returns the size of a file that was FTP'd via a calling script. The new
PERL script needs to connect to a secure server using Net::SFTP and RSA keys which is
working. However, I can not fine the equivalent of the Net::FTP cwd and size commands
for the Net::SFTP script. How does one do these functions?
#!/usr/bin/perl
use Net::FTP;
# Remote host variables passed by the caller
# Calling sequence: checkshopatronfiles.pl arg1 arg2 arg3 arg4 arg5
chomp($rhost=@ARGV[0]); # Remote host domain, alias or IP add
+ress
chomp($ruserid=@ARGV[1]); # Remote userid
chomp($rpasswd=@ARGV[2]); # Remote password
chomp($rdirectory=@ARGV[3]); # Remote directory
chomp($rfilename=@ARGV[4]); # Remote file name
# Local variables for LOG file name
# $log1=substr ($rfilename, 0, rindex($rfilename, "."));
$log1=$rfilename; # Use name as passed with no extension
$log2=".lg";
$lfilename=$log1.$log2; # Log file name
# Local variables for OUT file name
# $out1=substr ($rfilename, 0, rindex($rfilename, "."));
$out1=$rfilename; # Use name as passed with no extension
$out2=".sz";
$ofilename=$out1.$out2; # Output file name
open (LOGFILE, '>', $lfilename); # Always create the log file
print LOGFILE "Remote host=$rhost\n";
print LOGFILE "Remote userid=$ruserid\n";
print LOGFILE "Remote passwd=$rpasswd\n";
print LOGFILE "Remote directory=$rdirectory\n";
print LOGFILE "Remote filename=$rfilename\n";
open (OUTFILE, '>', $ofilename); # Always create the output file
$ftp=Net::FTP->new($rhost,Timeout=>10) or $errcd=1;
push @ERRORS, "Can't connect to $rhost\n" if $errcd;
myerr() if $errcd;
print LOGFILE "Connected\n";
$ftp->login($ruserid,$rpasswd) or $errcd=2;
push @ERRORS, "Can't login to $rhost\n" if $errcd;
myerr() if $errcd;
print LOGFILE "Logged in\n";
$ftp->cwd($rdirectory) or $errcd=3;
push @ERRORS, "Can't cd to $rdirectory\n" if $errcd;
myerr() if $errcd;
print LOGFILE "Found directory\n";
$ftp->binary;
$rfilesize=$ftp->size($rfilename) or $errcd=4;
push @ERRORS, "Can't get file size for file $rfilename\n" if $errcd;
myerr() if $errcd;
print OUTFILE "$rfilesize\n";
print LOGFILE "Got file size\n";
print LOGFILE "Remote filesize=$rfilesize\n";
print LOGFILE "Exit code=0\n";
close (LOGFILE);
close (OUTFILE);
$ftp->quit;
exit $errcd;
sub myerr {
print LOGFILE "Error=$errcd\n";
print LOGFILE @ERRORS;
close (LOGFILE);
print OUTFILE "0\n";
close (OUTFILE);
exit $errcd;
}
Here is the Net::SFTP test script. I did a put to make sure the secure login with
rsa keys is working and I can see the file on the remote server no problem.
However this is where the trouble begins.
I would like to change directory to where I put the file so I can get its size as
I did in the Net::FTP script.
#!/usr/bin/perl
use Net::SFTP;
use warnings;
$rhost="droppoint.shopatron.com";
$ruserid="1392";
$rdirectory="/uploads/inventory/dev";
$rfilename="/home/spch/xxx";
%args = (user => "$ruserid", ssh_args => {identity_files => ["/home/ta
+lex/.ssh/id_rsa"]});
print "rhost=$rhost\n";
print "ruserid=$ruserid\n";
print "rdirectory=$rdirectory\n";
print "rfilename=$rfilename\n";
$sftp=Net::SFTP->new($rhost, %args) or die "can't log in";
$sftp->put('/home/spch/xxx', '/uploads/inventory/dev/xxx');
$sftp->setcwd('/uploads/inventory/dev');
$sftp->size('/uploads/inventory/dev/xxx');
$sftp->quit;
exit;
When I run the script I get the following error messages:
Did we do something wrong on the installation of Net::SFTP or are the setcwd and size commands not supported?
Can't locate object method "setcwd" via package "Net::SFTP" at /home/spch/testperl.pl line 15.
If I comment out the setcwd command and try to get the file size using the full path name I get the following error.
Can't locate object method "size" via package "Net::SFTP" at /home/spch/testperl.pl line 16.
I also get the following error message when I comment out the setcwd and size commands.
Can't locate object method "quit" via package "Net::SFTP" at /home/spch/testperl.pl line 17.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.