9

Could anybody please tell me why this is not working?

#!/bin/bash
cd /home
touch somefile
/usr/bin/expect<<FILETRANSFER
spawn scp -r -P remoteServerPort somefile remoteServerIP:/home
expect "assword:"
send "MyPassWord\r"
interact
FILETRANSFER
echo "It's done"

It doesn't give any error but file is not transferred to remote server.I have tried many ways still couldn't find any solution.

5
  • A better way would be to avoid typing or sending any password with ssh (also scp ). Read some ssh tutorial and use a public key. Commented Jul 17, 2017 at 13:06
  • /usr/bin/expect<<FILETRANSFER, here you are missing a space before << Commented Jul 17, 2017 at 13:26
  • thanks for your replies. @ Basile Starynkevitch I can't use public key for some reasons. @ Jahid added space but still not working. Commented Jul 17, 2017 at 13:56
  • use /usr/bin/expect -d and see what expect is doing. Commented Jul 17, 2017 at 14:08
  • This script looks a bit "pseudo" -- are you using shell variables inside the expect body? Normally I would do expect <<'FILETRANSFER' where the single quotes effectively quote the entire here-doc. Commented Jul 17, 2017 at 14:10

1 Answer 1

17

The bash script you have defined is passing the expect commands on the standard input of expect. However, the expect command requires its arguments on a file or as an argument using the -c option.

You have several options but to add the less modifications on your script you just need to use the process substitution to create a here-document (temporary) for the expect command.

#!/bin/bash 

  echo "[DEBUG] INIT BASH"

  cd /home
  touch somefile

  /usr/bin/expect <(cat << EOF
spawn scp -r -P remoteServerPort somefile remoteServerIP:/home
expect "Password:"
send "MyPassWord\r"
interact
EOF
)

  echo "[DEBUG] END BASH"
Sign up to request clarification or add additional context in comments.

4 Comments

Btw, I have just checked and there is a SO answer with all the available options here: stackoverflow.com/questions/41165719/…
No, expect is perfectly fine reading the commands from stdin as posted in the question.
Is there any way to do this with Bourne Shell?
Actually this same script should work in sh since there is nothing bash dependant.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.