please advice why

spawn  scp  $FILE1   $FILE2  $LOGIN@$IP:/tmp

in my expect script copy only FILE1 and not copy FILE2 ?

I try to to transfer both files by scp as

 scp file1.csv file2.crt 

without expect and they transferred successfully to /tmp

so why VIA expect the only file that copied is FILE1 ??

what wrong in my syntax ?

example of my expect script:

 #!/usr/bin/expect -f
 set FILE1  file1.csv
 set FILE2  file2.crt
 set multiPrompt {[#>$]}
 spawn  scp  $FILE1   $FILE2  $LOGIN@$IP:/tmp
           expect {
                     ")?"   { send "yes\r"  ; exp_continue  }

                    word:  {send $PASS\r}

I also try this:

spawn  scp  "$FILE1   $FILE2"   $LOGIN@$IP:/tmp


spawn  scp  '$FILE1   $FILE2'   $LOGIN@$IP:/tmp

but I get the same problem

  • 1
    Note that, by default, expect's patterns are glob patterns (documented in string match), so ")?" means "match a close parenthesis followed by any character". Is this what you're intending to match? While you're developing your script, add exp_internal 1 as the first line following the shebang. – glenn jackman Aug 12 '14 at 20:29
  • You definitely do not want to put quotes around $FILE1 $FILE2 – glenn jackman Aug 12 '14 at 20:32
  • You don't declare any of LOGIN, PASS, IP in your script. Where do those values come from? – glenn jackman Aug 12 '14 at 20:33
  • I tried the same thing and it worked for me: #!/usr/bin/expect set FILE1 foo set FILE2 bar spawn scp $FILE1 $FILE2 root@ expect { "Password:" { send "password\r"} } expect { "\n" {} } Try a simpler command like echo maybe? What does it print when it runs; spawn prints its invocation? – eewanco Aug 12 '14 at 20:57
  • You can use the -o StrictHostKeyChecking=no scp option to avoid the first query you're trying to match on and answer (")?"). Any expect exchange you can avoid will make your script more reliable. What happens if you add the -v option to scp? Have you tried changing scp to /usr/bin/scp just to be paranoid? – eewanco Aug 13 '14 at 14:26

I've written the following expect script for this, and it works (on my vms). Sample run:

./scp.exp <first host user> <first host user pass> <first host name> <second host name> <second host user> <second host user pass> <directory path i.e. /tmp>


#!/usr/bin/expect -f
# ./sshlogin.exp uptime
# set Variables

set user [lindex $argv 0];
set password [lindex $argv 1];
set host [lindex $argv 2];
set copy_to_host [lindex $argv 3];
set copy_to_host_user [lindex $argv 4];
set copy_to_host_pass [lindex $argv 5];
set copy_to_host_dir [lindex $argv 6];
set file1 one.txt;
set file2 two.txt;
set timeout 10
# now ssh
spawn ssh $user@$host -o StrictHostKeyChecking=no
match_max 100000 # Look for passwod prompt
expect "*?assword:*"
# Send password aka $password
send -- "$password\r"
# send blank line (\r) to make sure we get back to gui
expect "*$ "
send -- "scp -o StrictHostKeyChecking=no $file1 $file2 $copy_to_host_user@$copy_to_host:$copy_to_host_dir\r"
expect "*?assword:*"
send -- "$copy_to_host_pass\r"
expect "*$ " 
send -- "exit\r"
expect eof

Note: you might need to add some wait time to expect if dealing with large files.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.