使用expect,使用scp生成

时间:2013-05-30 17:43:34

标签: shell expect scp spawn

我目前正在尝试使用带有shell脚本和expect包的scp命令自动执行文件传输。基于我所看到的,似乎我应该拥有     #!/usr/bin/expect -f 但是当我这样做时,我仍然得到错误:

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

我的代码就是这样做的:

#!/usr/bin/expect -f

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

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

2 个答案:

答案 0 :(得分:1)

将密码存储在脚本或任何其他文件中是一种不好的做法。请改用SSH身份验证密钥。

看看this tutorial

答案 1 :(得分:0)

看起来您正在调用sh DirectoryChange.sh之类的期望脚本。显然,sh不是 expect 脚本的正确解释器。

  • 更改文件扩展名:“。sh”适用于shell脚本
  • 确保它具有执行权限,然后使用./DirectoryChange.exp
  • 启动它
  • repository=$PWD"/subdirectory/"不是如何在expect中分配变量。删除此行并将cd行修改为cd subdirectory
  • 您不必与scp交互,因此请将最后一行更改为expect eof
相关问题