0

I have written an expect program (refer below:), where I am trying to copy files to remote machine. While scp'ing, if I provide wrong password, my expectation is second condition will be met and print the error and program will exit out.

However, what has ended up happening is, regardless I provide correct password or not it is meeting all conditions and program erroring out.

Am I doing something wrong here? Any help would be really appreciated.

spawn scp -p -o StrictHostKeyChecking=no $x $y ${toolpath}/cmd.sh $user@$ip:/x/home/$user
while (1) {
expect {
        "?assword:" {
                send_log "Password entered for scp'ing to $ip\n"
                send  "$password\n"
                }
        "Permission denied, please try again" {
                send_user "$ip : You have entered wrong password, Permission denied\n"
                exit 1
                }
        "timeout" {
                send_user "$ip : Failed to scp - timed out\n"
                exit 1
                }
        "lost connection" {
                send_user "$ip : Connection lost\n"
                exit 1
                }
        }
}
2
  • 2
    Is there any reason why you aren't using key-based authentication instead of entering a password?
    – cas
    May 26, 2016 at 8:02
  • As @cas said, use key based auth, and watch out for scp exit status. Far more simple. "The scp utility exits 0 on success, and >0 if an error occurs." May 26, 2016 at 9:52

1 Answer 1

0

You're just missing exp_continue in the password block. That will essentially loop back to the beginning of the expect command so you can wait for one of the other patterns.

But seriously, use ssh key authentication. It's way easier.

2
  • Thanks guys for your response. We do use 2FA and setting up ssh key authentication in production is against corporate security policies (unless I get exception) and hence can't set it up :(
    – Pat
    May 27, 2016 at 17:34
  • After adding exp_continue, if I put correct password it still goes back to Password block and erroring out.
    – Pat
    May 27, 2016 at 22:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .