ravi45722 has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to get files from server to another server through sftp. I am using this code upto now. But if the host is not connected its die and shows  Unable to establish SFTP connection. But i need to raise an alarm (email) if its not connected through sftp.

#!/usr/bin/perl use warnings; use strict; use Net::SFTP::Foreign; my $host = 'xxx.xx.x.xxx'; #Host adress my $password = 'teledna'; #Password for host my $path = "/root/sanjay/"; #Path where .csv file present #connecting to host using SFTP my $sftp = Net::SFTP::Foreign->new($host, password => $password); $sftp->die_on_error("Unable to establish SFTP connection"); #Changing the path to .csv directory $sftp->setcwd($path) or die "unable to change cwd: " . $sftp->error; #get("Name of file in remote server","Name you have to given to file") $sftp->get("database.csv", "database.csv") or die "get failed: " . $sf +tp->error;

Replies are listed 'Best First'.
Re: Need alarm while getting error on SFTP
by choroba (Cardinal) on Dec 03, 2015 at 07:32 UTC
    Instead of die_on_error, check for the error yourself:
    my $sftp = Net::SFTP::Foreign->new($host, password => $password); if ($sftp->error) { # Send the email. }

    If you also need help sending email from Perl, see Task::Kensho::Email.

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

      Thanks. It's working. I searched the manual pages pages for Timeout conditions while doing sftp. but I dont get any. How to give timeout condition in this module like below??

       $ftp=Net::FTP->new($primary_ip,Timeout=>120) or $connect_err=1;
        Next time, try searching the documentation of the module you're actually using: Net::SFTP::Foreign:
        timeout => $seconds

        when this parameter is set, the connection is dropped if no data arrives on the SSH socket for the given time while waiting for some command to complete.

        When the timeout expires, the current method is aborted and the SFTP connection becomes invalid.

        Note that the given value is used internally to time out low level operations. The high level operations available through the API may take longer to expire (sometimes up to 4 times longer).

        ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,