【问题标题】:My expect script closes too soon我的期望脚本关闭得太早
【发布时间】:2018-01-23 02:50:12
【问题描述】:

我正在尝试使用 expect 脚本将 borg 备份发送到远程服务器并修剪旧的。发送备份的脚本工作得很好。但是,在修剪任何备份之前修剪存档的脚本会超时。我已经确认手动运行该命令确实有效。关于它为什么超时的任何想法?

#!/usr/bin/expect

set folder [exec bash -c "ls -td /home/.snapshots/* | head -n 1"];
set otp [exec oathtool --totp -b KEY];

spawn borg prune --keep-hourly 5 --keep-daily 7 --keep-weekly 4 --keep-monthly 3 --keep-yearly 1 --prefix='home' ssh://user@host:PORT/ARCHIVE

expect "Enter passphrase for key '/root/.ssh/id_ed25519': "
send -- "PASSWORD\r"

expect "Verification code: "
send -- "$otp\r"

expect "Enter passphrase for key ssh://user@host:PORT/ARCHIVE: "
send -- "PASSWORD\r"

expect eof
wait

【问题讨论】:

  • 你为什么用expect来回答SSH密码提示?你真的不应该那样做。
  • 当我发现任何问题时,我会使用 autoexpect 生成脚本,然后对其进行修改以简化。
  • @JohnKugelman 你知道将 borg 备份发送到远程存档的更好方法吗?我非常愿意接受建议。
  • 配置好SSH,不会有任何提示。它的提示仅供人类食用。例如,用于连接的私钥上的get rid of the passphrase。文件系统权限应该足以防止密钥被窥探。 (在期望脚本的其他地方列出的密码是没有用的。)

标签: linux bash ssh expect


【解决方案1】:

默认的期望超时为 10 秒,如果您的剪枝操作运行超过 10 秒且未产生任何输出,则期望将超时。

您应该在脚本开始时将超时设置为 2 分钟,例如:

#!/usr/bin/expect

set folder [exec bash -c "ls -td /home/.snapshots/* | head -n 1"];
set otp [exec oathtool --totp -b KEY];
set timeout 120
[ ... ]

您也可以将您的期望脚本设置为 DEBUG,以便能够看到它在未来失败的原因:

#!/usr/bin/expect -d

【讨论】:

  • set timeout -1 禁用超时。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-05-29
  • 2017-05-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-20
  • 1970-01-01
相关资源
最近更新 更多