Neagu V
Neagu V

Reputation: 480

Expect script for scp 2 remotes does not copy all the files

I have 2 remote machines lets call them A and B.

I want to transfer files from A to B.

My expect script:

#!/usr/bin/expect
set cmd [lindex $argv 0]
set password1 [lindex $argv 1]
set password2 [lindex $argv 2]
spawn bash -c "$cmd"
expect {
-re ".*es.*o.*" {
exp_send "yes\r"
exp_continue
}
-re ".*sword.*" {
exp_send "$password1\r"
exp_continue
}
-re ".*sword.*" {
exp_send "$password2\r"
}
}

The expect script is used in a shell script:

 expect $my_path/my_expect_script.exp "scp -r $remote_A_user@$remote_A_host:$file_path/* $remote_B_user@$remote_B_host:$file_path" $remote_A_password $remote_B_password 

Always the script returns an error when entering password for the first time, in the same execution the next attempt works.

yes
[email protected]'s password: 
Permission denied, please try again.

Sometimes some files are not copied to remote B.

Do you know how can I manage to execute a scp expect script on 2 remotes.

Thanks,

Upvotes: 0

Views: 144

Answers (1)

glenn jackman
glenn jackman

Reputation: 247082

First, after you authenticate successfully, there are no more commands in the script, so expect exits and that kills scp. To wait for scp to finish, you have to wait for eof

Next, assuming the file transfer takes more than 10 seconds, you should set the timeout value to Infinity: set timeout -1

Last, the 2nd password will never be sent: the first password prompt will always match first. If those are meant to be two distinct password prompts, you need to make them match some unique text about the different hosts.

Also, indentation aids comprehension

spawn bash -c "$cmd"
set timeout -1
expect {
    -re ".*es.*o.*" { # <== this is meaningless to the reader: add a whole word
        exp_send "yes\r"
        exp_continue
    }
    -re ".*sword.*" { # <== need something more here
        exp_send "$password1\r"
        exp_continue
    }
    -re ".*sword.*" { # <== need something more here
        exp_send "$password2\r"
        exp_continue
    }
    eof
}

Upvotes: 1

Related Questions