【问题标题】:expect script inside bash script期望 bash 脚本中的脚本
【发布时间】:2014-09-20 07:11:18
【问题描述】:

我真的很难过! 我不想使用 RSA AUTH,所以请弃权。

#!/bin/bash

echo "Enter the server password" 
read password 
cd /home/mike/ 

# this is included the code below -> 
/usr/bin/expect << EOD
set timeout -1
spawn scp file.txt server.com:/home
expect {
  timeout            { send_user "TIME OUT\n"; exit 1 }
  "*assword: "
}
send "$password\r"
expect {
  "s password: " { send_user "\nIncorrect Password. Login Failed.\n"; exit 1 }
  "100%"
}
sleep 1
send "exit\r"
expect eof

这有效,file.txt 被传输到服务器,但我收到此警告消息 ->

" line 44: warning: here-document at line 22 delimited by end-of-file (wanted `EOD')"

当我在“期望 eof”之后添加 EOD 时,它给了我这个错误 ->

send: spawn id exp4 not open
    while executing
"send "exit\r"

任何帮助将不胜感激。 我再说一遍我不能使用 pubkey auth 所以请不要建议。

【问题讨论】:

  • 您为什么要向scp 会话发送“退出”?当你直接使用scp 时,你会这样做吗?

标签: linux bash expect scp


【解决方案1】:

通过替换你的最后一行:

expect eof

与:

EOD

我不再收到投诉了。请注意,您的 EOD 标记(您在最初的 /usr/bin/expect 行中定义的标记)之前不能有任何制表符(可能还有空格?)。

【讨论】:

  • 不,不要替换:添加 EOD 行。
  • @julumme 谢谢 :) 感谢您的帮助。在 EOD 之前我有一个空间。甚至考虑一下。
  • @glennjackman 是的,不需要更换它。 :) 谢谢 :)
【解决方案2】:

您可以简单地使用 curl 通过 scp 或 sftp 将文件复制到远程主机或从远程主机复制文件,而不是尝试使用 expect 编写脚本,并将身份验证凭据传递给 curl 命令,如下所示:

curl sftp://remotehost.domain.com/path/to/remote/file --user username:password -o /path/to/local/file

【讨论】:

  • 哇!也会试试这个。谢谢。有新东西要向你学习 :) 竖起大拇指 :)
猜你喜欢
  • 2016-02-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-11
  • 1970-01-01
  • 2018-07-05
  • 2020-04-08
  • 1970-01-01
相关资源
最近更新 更多