Design the future
Design the future

Reputation: 15

scp in Expect script works for few files but not for many files

I wrote a bash code to send files to a sever computer from my remote laptop. I used 'scp' command and wrote it on a bash script to bypass entering a password every time I ran it.

expect <<EOF
spawn scp -P 1111 -o StrictHostKeyChecking=no -r /Users/Desktop/sync_mac [email protected]:/home/folder
expect "password:"
        send "11111\r"
expect eof
EOF

However, the problem is when I ran the bash script on the terminal, it seemed like working well but suddenly failed sending files without any sign of warnings.(Especially for the case of sending a large number of files or a large size of file, it was okay for the case of a small number and a small size)

Thanks for your help

Upvotes: 1

Views: 985

Answers (2)

pynexj
pynexj

Reputation: 20798

The default timeout for expect is 10 seconds so expect eof would wait for at most 10 seconds which may be not enough for many files as you mentioned.

To fix, you can set timeout -1 before expect eof or just expect -timeout -1 eof.

Upvotes: 1

user9987379
user9987379

Reputation:

I had to do this a while back, but for multiple servers, the list of which would change from night to night. I discovered sometimes scp would ask to add the host to your list of known hosts first, or other messages for which I had to code handling. That could be getting buried in your script.

This is an excerpt of what ultimately worked, if that helps:

        expect -c "
                spawn ssh-copy-id -i /x/home/$USER/.ssh/id_rsa.pub  $USER@$HOST
                expect {
                        \"password:\" {
                                send \"$PASS\n\"
                                expect {
                                        \"expecting.\" { }
                                        timeout {exit 1}
                                        \"again.\" {exit 1}
                                }
                        }
                        \"yes/no)?\" { 
                                send \"yes\n\" 
                                expect \"password:\" {
                                        send \"$PASS\n\"
                                        expect {
                                                \"expecting.\" { }
                                                timeout {exit 1}
                                                \"again.\" {exit 1}
                                        }
                                }
                        }                
                }

Upvotes: 0

Related Questions