Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Net::SFTP Object Methods?

by talex (Initiate)
on Aug 14, 2013 at 18:12 UTC ( #1049453=perlquestion: print w/ replies, xml ) Need Help??
talex has asked for the wisdom of the Perl Monks concerning the following question:

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.

Comment on Net::SFTP Object Methods?
Select or Download Code
Replies are listed 'Best First'.
Re: Net::SFTP Object Methods?
by runrig (Abbot) on Aug 14, 2013 at 18:20 UTC
    Where did you see that setcwd() or size() were valid methods in Net::SFTP (Update: You need to call one of the stat methods which returns an attribute object which you can call size() on)? I'd recommend Net::SFTP::Foreign over Net::SFTP anyway. But make sure you read the docs and only call methods that exist.
Re: Net::SFTP Object Methods?
by glasswalk3r (Pilgrim) on Aug 14, 2013 at 18:25 UTC

    I think you should try a couple of things:

    1. Read How do I post a question effectively?
    2. Review your node text.
    3. Connect to the SFTP server of yours with a regular interactive client, read the documentation of it and then check how this could be done. Later, you can check additional methods from Net::SFTP that could help you.
    Alceu Rodrigues de Freitas Junior
    ---------------------------------
    "You have enemies? Good. That means you've stood up for something, sometime in your life." - Sir Winston Churchill

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1049453]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2015-07-08 06:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (94 votes), past polls