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.


In reply to Net::SFTP Object Methods? by talex

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.