문제

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
도움이 되었습니까?

해결책 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

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top