以下expect脚本将删除远程计算机上的文件/var/tmp/file
但在此之前,expect 脚本会在远程计算机上执行 ssh 操作,
我放置 2>/tmp/errors
是为了捕获来自 ssh 的错误
但我注意到,尽管 ssh 远程发送错误,我没有看到 /tmp/errors
文件中的错误
但是当我尝试手动时
ssh $LOGIN@$machine
然后 ssh 失败警告:远程主机标识已更改
但从预期来看,我无法在 /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"
最佳答案
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"
关于linux - 如何从expect脚本中捕获标准错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26736886/