I'm trying to run a command as root (via sudo) on a remote server, and capture stdout (but not stderr) into a file.
eg. Something like this:
ssh user@remote "cat /root/file.tar | gzip" > root-file.tar.gz
Except I need to be root on the remote:
ssh user@remote "sudo cat /root/file.tar | gzip" > root-file.tar.gz
root has no password and can't be logged in as, so I can't use
When I try the above command, I get:
$ ssh user@remote "sudo cat /root/file.tar | gzip" > root-file.tar.gz sudo: no tty present and no askpass program specified
-t so that ssh allocates a tty, but then
root-file.tar.gz gets the output from sudo (and gzip, but I can --force), so I guess it's capturing remote stdout and stderr into local stdout:
$ ssh -t user@remote "sudo cat /root/file.tar | gzip" > root-file.tar.gz (hangs) $ cat root-file.tar.gz gzip: compressed data not written to a terminal. Use -f to force compression. For help, type: gzip -h [sudo] password for user:
I've white-listed specific commands using NOPASSWD in sudoers, which works well for regularly-occurring scripts, but I find that I want to do this sort of thing occasionally with different arbitrary commands so I can't be forever adding NOPASSWD.