1

I'm working on a shell script and I'm trying to send a backup file to a sftp server :

remotepath=/backup/
expect -c "
spawn sftp [email protected]
expect "password:"
send "password123\r"
expect "sftp>"
send "put /back_up/bkp_2022.tar.gz $remotepath\r"
expect "sftp>"
send "bye\r"
"

And this is the output that I get without the file being transfer - It just creates "?send put" and "?send byer?".

-rw-r-----. 1 admin admin 1267 Aug 17 11:14 script.sh
-rw-r-----. 1 admin admin    0 Aug 17 11:15 ?send put
-rw-r-----. 1 admin admin  167 Aug 17 11:15 ?send byer?

Can you please help ?

2
  • Could you use scp instead of sftp? Commented Aug 17, 2022 at 10:00
  • If the transfer can take more than 10 seconds, add set timeout -1 to the top of the expect code. Commented Aug 23, 2022 at 17:43

1 Answer 1

1

either escape inner quote ("), or use here doc syntax

remotepath=/backup/
expect <<EOF
spawn sftp [email protected]
expect "password:"
send "password123\r"
expect "sftp>"
send "put /back_up/bkp_2022.tar.gz $remotepath\r"
expect "sftp>"
send "bye\r"
EOF

please note that I didn't test

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.