【问题标题】:How to catch standard errors from expect script如何从期望脚本中捕获标准错误
【发布时间】:2014-12-31 10:42:06
【问题描述】:

以下期望脚本将删除远程机器上的文件 /var/tmp/file

但在此之前,expect 脚本会在远程机器上执行 ssh,

我放了2>/tmp/errors 以便从 ssh 捕获错误

但我注意到尽管 ssh 远程发送错误,但我没有看到来自 /tmp/errors 文件的错误

但是当我尝试手动时

ssh   $LOGIN@$machine

然后 ssh 在WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED 上失败

但我无法在/tmp/erros 中发现此错误

请指教出了什么问题?为什么2>/tmp/errors 不捕获错误?

 expect_test=`cat << EOF
 set timeout 50
 spawn  ssh   $LOGIN@$machine  2>/tmp/errors
       expect {
                 ")?"   { send "yes\r"  ; exp_continue  }

                 word:  { sleep 1 ; send $PASSORD\r}
              }
 expect >  {send "sleep 1\r"}
 expect >  {send "rm -f /var/tmp/file\r"}
 expect >    {send exit\r}
 expect eof
 EOF` 


 expect -c  "$expect_remove_file"

【问题讨论】:

  • 嗨,Etan,你好吗 - 感谢更新
  • 我不知道你是否可以在 spawn 这样的命令中使用重定向。如果可以的话,您可能至少需要为 tcl 引用它。话虽这么说,如果 expect 没有读取标准错误(如果确实如此,我不记得了),您可能可以直接将其粘贴在 expect -c 执行上以获得您想要的。
  • 复制到超级用户:superuser.com/q/835794/4714

标签: linux bash shell ssh expect


【解决方案1】:

spawn 不理解 I/O 重定向。替换

spawn  ssh   $LOGIN@$machine  2>/tmp/errors

与任一

spawn  ssh   $LOGIN@$machine -E /tmp/errors
# -E log_file tells ssh where to write the error log instead of stderr

spawn  sh -c "ssh $LOGIN@$machine 2>/tmp/errors"

【讨论】:

    猜你喜欢
    • 2011-07-05
    • 1970-01-01
    • 2021-06-24
    • 2016-06-09
    • 1970-01-01
    • 2012-11-03
    • 2011-04-17
    • 1970-01-01
    相关资源
    最近更新 更多