bash expect 脚本中的命令重定向因权限被拒绝而失败(spawn ssh user@host 命令 > 输出文件)

bash command redirection in expect script failing with permission denied (spawn ssh user@host command > outfile)

我有这个expect脚本

#!/usr/bin/expect -f
set pass [ exec echo my_phrase | gpg --batch --quiet --yes --passphrase-fd 0 -d /root/.password-store/ssh/my_pgp_store.gpg ]
spawn ssh -oStrictHostKeyChecking=no -oCheckHostIP=no -i /home/myuser/.ssh/id_rsa ssh-user@remote-host.com /some/remote/script > /home/myuser/ssh.output
expect "*?assword:*"
send -- "$pass\r"
interact

应该读取 gpg 加密密码,并使用它通过 ssh 调用远程脚本(我需要这样做的原因有很多)。现在,我的问题是,每当我 运行 这个脚本时,ssh 连接都已正确设置,但它无法写入 ssh 调用的输出,错误为

bash: /home/myuser/ssh.output: Permission denied

我运行将脚本设置为 root(因为我必须运行它是 root),并且我已经尝试更改所有相关文件和目录的权限和所有权.

redir char > 对于 expect 的 spawn 命令来说并不特殊。命令

spawn ssh user@host command > outfile

的含义相同
spawn ssh user@host "command > outfile"

并且 redir > outfile 将在远程主机上执行。

这样试试:

spawn bash -c "ssh user@host command > outfile"
expect "assword:"
send -- "$pass\r"
interact