I'm trying to run a command over SSH like this in a here-document:
ssh example.com <<END
sudo /etc/init.d/apache2 reload
END
Which normally works except when I'm trying to run something that needs input, such as the password for sudo. In that case, I'm getting the following message:
sudo: no tty present and no askpass program specified
I know I can use the -t
flag on SSH to allocate a pseudo-tty such as this:
ssh -t example.com "sudo /etc/init.d/apache2 reload"
And it'll work, but when I try the same thing with here-document, it doesn't work and I'll get the same error about no tty being present:
ssh -t example.com <<END
sudo /etc/init.d/apache2 reload
END
Any idea how I can make this work?
Also, in case you are wondering why I want it to work with the here-document instead of just passing it on the same line, it is because the input commands (there can be a few) come from a configuration file read by a script and I've heard it avoids the trouble of escaping the commands for the quotes, double quotes, etc.
Use
visudo
to edit the sudoers file and insert a below line:Try
ssh -t -t
to force pseudo-tty allocation even if stdin is not a terminal.Why not simply store the here document in a variable that will be given
ssh -t
as a command argument.On a more general note, use
ssh -T
or specify the remote shell as a command argument if the stdin of the remote host is being redirected from a heredoc (to preventssh
from trying to allocate a pty).