如何从期望脚本中捕获标准错误

时间:2014-11-04 13:54:10

标签: linux bash shell ssh expect

以下expect脚本将删除远程计算机上的文件/ 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"

1 个答案:

答案 0 :(得分:2)

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"