# If sftp is not in your path replace with absolute path of sftp program 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 sftp command \n"; # If this is the first time you are running this , expect will send "yes" 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 = shift; $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 on 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(); }