Question

I'm currently trying to automate a file transfer using the scp command with a shell script and the expect package. Based on what I've seen it seems that I should have #!/usr/bin/expect -f But when I did that I still get the errors:

DirectoryChange.sh: line 33: spawn: command not found
couldn't read file "*Password:*": no such file or directory
DirectoryChange.sh: line 35: send: command not found
DirectoryChange.sh: line 36: interact: command not found

The code I have works something along these lines:

#!/usr/bin/expect -f

repository=$PWD"/subdirectory/"
set pass "***********"

cd $repository
spawn scp -r user@host:/copyDirectory/ .
expect "*Password:*"
send "${pass}\r";
interact
Was it helpful?

Solution 2

Looks like you're invoking your expect script like sh DirectoryChange.sh. Clearly sh is not the correct interpreter for an expect script.

  • change the file extension: ".sh" is for shell scripts
  • make sure it has execute permissions then launch it with ./DirectoryChange.exp
  • repository=$PWD"/subdirectory/" is not how to assign variables in expect. remove this line and edit the cd line to cd subdirectory
  • you don't have to interact with scp, so change the last line to expect eof

OTHER TIPS

It's a bad practice to store passwords in scripts or any other file. Use SSH authentication keys instead.

Take a look at this tutorial.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top