Skip to content
Advertisement

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

I have this expect script

#!/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 -- "$passr"
interact

that is supposed to read a gpg encrypted password, and use it to call a remote script via ssh (there are many reasons why I need to do it like that). Now, my problem is that whenever I run this script, the ssh connection is correctly set up, but it fails in writing the output of the ssh call with the error

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

I’m running the script as root (because I have to run it as root), and I already tried to change the permissions and ownerships of all the involved files and directories.

Advertisement

Answer

The redir char > is not special for expect’s spawn command. The command

spawn ssh user@host command > outfile

means the same as

spawn ssh user@host "command > outfile"

and the redir > outfile will be executed on the remote host.

Try like this:

spawn bash -c "ssh user@host command > outfile"
expect "assword:"
send -- "$passr"
interact
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement