Based on one of the topics posted here, i have designed my expect script to return the exit code but for some reason it keeps on running and doesnt exit at all
I am using the below script to -get a file from the remote server -delete the file after the transfer is complete
Below is the code written
set timeout 30
set user [lindex $argv 0]
set pass [lindex $argv 1]
set host [lindex $argv 2]
set remote_file [lindex $argv 3]
set local_file [lindex $argv 4]
set port [lindex $argv 5]
if {$port != "" } {
spawn sftp -oPort=$port $user@$host
} else {
spawn sftp $user@$host}
expect -nocase password:
send "$pass\r"
expect sftp>
send "get $local_file $remote_file\r"
expect sftp>
send "rm $local_file\r"
expect eof
set waitval [wait -i $spawn_id]
exit [lindex $waitval 3]
-> It should have exit with the return code.
I am using the below alternate script for now. It works. But I was hoping to make it more generic without mentioning the error messages. Please let me know if there is any improvements that can be done for the below script.
set timeout 30
set user [lindex $argv 0]
set pass [lindex $argv 1]
set host [lindex $argv 2]
set remote_file [lindex $argv 3]
set local_file [lindex $argv 4]
set port [lindex $argv 5]
if {$port != "" } {
spawn sftp -oPort=$port $user@$host
} else {
spawn sftp $user@$host}
expect -nocase password:
send "$pass\r"
expect {
-re "failed|invalid password|incorrect|denied" {exit 1}
"Connection closed" {exit 1}
timeout {exit 1}
"sftp>"
}
send "get $local_file $remote_file\r"
expect {
-re "not found|denied|error|failure" {exit 1}
"No such file or directory" {exit 1}
timeout {exit 1}
"sftp>"
}
send "rm $local_file\r"
expect {
-re "not found|denied|cannot remove|error|failure" {exit 1}
"No such file or directory" {exit 1}
"sftp>"
}
send "exit\r"
expect eof
Regards, Allen