I'm trying to get file information off an SFTP site, to ultimately move the files locally. My plan was to connect using pwd authentication, list the files in a specified directory, and for each result, determine if it is a file. If so, get the file and place it locally.

I can connect and get, but am having fits trying to figure out how to process the directory listing.

Below is the code I have. I have tried calling a subroutine from in sftp->ls and other things. No luck. Any suggestions would be welcome.

#!/var/opt/pgpdba/perl/bin/perl #use strict; use Net::SFTP; use Net::SFTP::Attributes; use Logger; use Platform; use Getopt::Std; use File::Basename; $0 =~ s/^.*(\/|\\)//; # Strip a path if it is exists my $root = $0; $root =~ s/\..*$//; # Clean up script name my $LOG_FILE = "./${root}.log"; getopts('hr:l:'); print_usage_and_quit() if ($opt_h); my $CREDFILE = "./user.txt"; my $host=""; my $user=""; my $password=""; my $rdir=$opt_r; my $ldir=$opt_l; openlog("$LOG_FILE"); load_credentials(); my $sftp = Net::SFTP->new($host, user => $user, password => $password, debug => 1); #my $attrs = Net::SFTP::Attributes->new(Stat => [ stat "./session_moni +tor.sql" ]); #my $size = $attrs->size; #print "$size\n"; #exit; $sftp->ls($rdir,sub{print "$_[0]->{filename}\n"}) ; #@listing = $sftp->ls($rdir,); #foreach(@listing) #{ # print "$_\n"; #}*/ #$sftp->do_lstat("oc") ; #$sftp->get("$rdir/*", "$ldir/*"); #$sftp->put("bar", "baz"); sub getfile ($filestuff) { if ($_[0]->{filename} ne '.' && $_[0]->{filename} ne '..' && $ +_[0]->{filename} ne 'processed') { print "1 - $filestuff\n" #$sftp->get("$rdir/".$_[0]->{filename}, "$ldir/".$_[0] +->{filename}); } } sub load_credentials { open (CREDFILE1, "<$CREDFILE") or quittext_opc "Can't open credentials file $CREDFILE\n" +, $AP_CNA, $SEV1, $GRP3, "1010", $STAT1; #die "Can't open credentials file $CREDFILE\n"; while (<CREDFILE1>) { # next if /^\s*(?:#|$)/; + # ignore comments/ blank lines chomp; my @fields = split /:/; if ($fields[0] eq 'user') { $user=$fields[1]; } if ($fields[0] eq 'pwd') { $password=$fields[1]; } if ($fields[0] eq 'host') { $host=$fields[1]; } } return 0; }

I didn't include the print_usage_and_quit sub....

Edited by davido: Added readmore tags.

Thanks, Jim

In reply to using sftp to get file information for processing by jblapham

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.