1#!/usr/bin/expect 2# usage: expect_scp <remote-ip-addr>:<remote-path> <local-path> 3 4# Sometimes, the network connection time may be more than the default 5# timeout duration of expect, i.e., 10 seconds. Hence, set a longer timeout. 6set timeout 20 7 8set local_path [lindex $argv 1] 9set remote_path [lindex $argv 0] 10spawn scp -r -o StrictHostKeyChecking=no -o UserKnownHostsFile="/tmp/null" \ 11 "$local_path" "$remote_path" 12 13# The prompts from the remote machine are not deterministic. 14# For example, it may prompt (yes/no) for 0, 1, or even 2 times; 15# it may or may not prompt the user for a password, etc. 16# We used a loop to handle various combinations of such possibilities. 17expect { 18 # The remote machine has closed the connection. 19 eof { 20 puts "Has copied the .version file to the remote machine." 21 exit 0 22 } 23 24 # The scp connection times out. 25 timeout { 26 send_error "Error: scp timeout!"; 27 exit 1 28 } 29 30 -nocase "(yes/no)?" { 31 send "yes\r"; 32 exp_continue 33 } 34 35 -nocase "password:" { 36 send "test0000\r"; 37 exp_continue 38 } 39 40 -nocase "Permanently added" {} 41 42 # This exception may occur when the .version file was not created for any 43 # reason. This causes the spawned process for scp to abort and thus 44 # this script was killed in a cascaded way. 45 -nocase "killed by signal" { 46 send_error "Error: the spawned scp process was killed.\n"; 47 exit 1 48 } 49 50 # This exception may occur when the machine IP is incorrect. 51 -nocase "lost connection" { 52 send_error "Error: lost connection to the chromebook machine.\n"; 53 exit 1 54 } 55} 56