Loading ...
Sorry, an error occurred while loading the content.
 

how to close SFTP connection?

Expand Messages
  • Jeff Shu
    Hello Everyone: I am uisng NET::SFTP module to access a remote server. Everyone 5 minutes, there is cron job calling my script to upload file onto the remote
    Message 1 of 3 , Sep 4, 2008
      Hello Everyone:

      I am uisng NET::SFTP module to access a remote server. Everyone 5
      minutes, there is cron job calling my script to
      upload file onto the remote server.

      I would like to knnow how can I close the SFTP connection without
      exiting my Perl script. I appreciate your advises and help!
      Would it be OK, If I don't close SFTP connection, will it affect the
      file transfer?

      My code snippet

      #!/usr/bin/perl
      use Net::SFTP;

      sub ftp_send() {
      my($file) = @_;
      $sftp = Net::SFTP->new('xxx', user=>'zzz', password=>'yyy',
      debug=>1);
      $sftp->put("$homedir", "$remote");
      #close the SFTP connection here or exit SFTP environment
      }

      if ($sth->rows > 0)
      {
      while($href = $sth->fetchrow_hashref()) {
      $optCount++;
      &build_file($href);
      }

      $filename = 'test.txt';
      open(out, ">>/home/$filename") || die("Couldn't open data file $!\n");
      print out $table;
      close(out);
      &ftp_send($filename);

      }
    • Charles K. Clarkson
      ... There is no concept for logging off or for closing the SFTP connection in the Net::SFTP docs or in the source code. Also, there is no DESTROY method in
      Message 2 of 3 , Sep 6, 2008
        Jeff Shu wrote:

        : I am uisng NET::SFTP module to access a remote server. Everyone 5
        : minutes, there is cron job calling my script to upload file onto the
        : remote server.
        :
        : I would like to knnow how can I close the SFTP connection without
        : exiting my Perl script. I appreciate your advises and help!
        : Would it be OK, If I don't close SFTP connection, will it affect the
        : file transfer?

        There is no concept for logging off or for closing the SFTP
        connection in the Net::SFTP docs or in the source code. Also, there is
        no DESTROY method in Net::SFTP or the underlying Net::SSH::Perl object
        so letting the $sftp object go out of scope may not work, but it's
        usually a good idea to limit the scope of all variables in a subroutine.



        : sub ftp_send() {
        : my($file) = @_;
        : $sftp = Net::SFTP->new('xxx', user=>'zzz', password=>'yyy',
        : debug=>1);
        : $sftp->put("$homedir", "$remote");
        : #close the SFTP connection here or exit SFTP environment
        : }

        sub ftp_send() {

        my( $file, $homedir, $remote ) = @_;

        my $sftp = Net::SFTP->new(
        'host',
        user => 'zzz',
        password => 'yyy',
        debug => 1
        );

        $sftp->put( $homedir, $remote );

        # When this sub routine ends the SFTP connection should close
        # as $sftp goes out of scope. If this sub routine is not ending
        # here, I could use "undef $sftp;" to destroy the object.
        }

        BTW:

        * Don't quote variables needlessly. "$remote" is not always the
        same as $remote. See What's wrong with always quoting "$vars"?
        in Perlfaq4.

        * Perlsub recommends that new perl programmers not use the "&"
        prefix on subroutines. &ftp_send($filename) is better called
        with ftp_send($filename) until you know why they are different.



        HTH,

        Charles K. Clarkson
        --
        Mobile Homes Specialist
        Free Market Advocate
        Web Programmer

        254 968-8328

        http://www.clarksonenergyhomes.com/wordpress/about/

        Don't tread on my bandwidth. Trim your posts.
      • Jeff Shu
        ... wrote: Hello Charles: Thanks for the explanation. I will use undef $sftp; in my ftp_send function.
        Message 3 of 3 , Sep 6, 2008
          --- In perl-beginner@yahoogroups.com, "Charles K. Clarkson"
          <cclarkson@...> wrote:
          Hello Charles:

          Thanks for the explanation. I will use undef $sftp; in my ftp_send
          function.

          > There is no concept for logging off or for closing the SFTP
          > connection in the Net::SFTP docs or in the source code. Also, there is
          > no DESTROY method in Net::SFTP or the underlying Net::SSH::Perl object
          > so letting the $sftp object go out of scope may not work, but it's
          > usually a good idea to limit the scope of all variables in a subroutine.
          >
          >
          >
          > : sub ftp_send() {
          > : my($file) = @_;
          > : $sftp = Net::SFTP->new('xxx', user=>'zzz', password=>'yyy',
          > : debug=>1);
          > : $sftp->put("$homedir", "$remote");
          > : #close the SFTP connection here or exit SFTP environment
          > : }
          >
          > sub ftp_send() {
          >
          > my( $file, $homedir, $remote ) = @_;
          >
          > my $sftp = Net::SFTP->new(
          > 'host',
          > user => 'zzz',
          > password => 'yyy',
          > debug => 1
          > );
          >
          > $sftp->put( $homedir, $remote );
          >
          > # When this sub routine ends the SFTP connection should close
          > # as $sftp goes out of scope. If this sub routine is not ending
          > # here, I could use "undef $sftp;" to destroy the object.
          > }
          >
          > BTW:
          >
          > * Don't quote variables needlessly. "$remote" is not always the
          > same as $remote. See What's wrong with always quoting "$vars"?
          > in Perlfaq4.
          >
          > * Perlsub recommends that new perl programmers not use the "&"
          > prefix on subroutines. &ftp_send($filename) is better called
          > with ftp_send($filename) until you know why they are different.
          >
          >
          >
          > HTH,
          >
          > Charles K. Clarkson
          > --
          > Mobile Homes Specialist
          > Free Market Advocate
          > Web Programmer
          >
          > 254 968-8328
          >
          > http://www.clarksonenergyhomes.com/wordpress/about/
          >
          > Don't tread on my bandwidth. Trim your posts.
          >
        Your message has been successfully submitted and would be delivered to recipients shortly.