bhavanavm has asked for the wisdom of the Perl Monks concerning the following question:
# 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(); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: EXPECT SFTP | Trap Exceptions
by salva (Canon) on Mar 13, 2012 at 21:18 UTC |