We have a module that securely transfers files to an external vendor using Perl Script ( EXPECT Module) .. It appears like this code has been copied from an example that is available in one of the perl online tutorials. For FTP Authentication Failure, I would like to trap the error and throw an exception.. However, the process seems to be not throwing an error (in other words, I am not able to identify how to trap the error) .. Code Snippet attached below.
# If sftp is not in your path replace with absolute path of sftp progr +am my $COMMAND = 'sftp'; my $FTP_PARAMS = ("$SFTP_USER\@$SFTP_ADDRESS"); # Create the Expect object my $exp = Expect->spawn($COMMAND, $FTP_PARAMS) or die "Cannot spawn sf +tp command \n"; # If this is the first time you are running this , expect will send "y +es" to add the key # for the sftp server to the ~/.ssh/known_hosts file else # wait for "Password Authentication" string to show up $exp->expect($TIMEOUT, ["Password Authentication"], ["Are you sure you want to continue connecting", sub {my $self = s +hift; $self->send("yes\n");}] ); # Wait for Password prompt to show up $exp->expect($TIMEOUT, ["Password:"]); # Sent the sftp password $exp->send("$SFTP_PASSWORD\n"); # Wait for sftp prompt $exp->expect($TIMEOUT, ["sftp>"]); if (lc($FUNCTION) eq "get" ) { # Get yesterday's report file/s and put it in reportsPath directory o +n the local machine $exp->send("get $FILE_NAME $WORKING_DIRECTORY\n"); } else { # Get yesterday's report file/s and put it in reportsPath directory +on the local machine $exp->send("put $FILE_NAME $WORKING_DIRECTORY\n"); } # Wait for sftp prompt $exp->expect($TIMEOUT, ["sftp>"]); # Close ftp session $exp->send("bye\n"); # Destroy the expect object $exp->soft_close(); }

In reply to EXPECT SFTP | Trap Exceptions by bhavanavm

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.