Cat a file across ssh?

There is a file on a remote host that I want to read across an ssh tunnel. NOT copy... scp won't work here. And this file requires elevated permissions to read.

'ssh -t remotehost sudo cat /path/to/file' will prompt me for my sudo password and read out the file. But I'm coming up blank for a way to capture that output. I really just need to grep and cut and sed my way through the output, so it doesn't matter how the content is available locally... writing to a temporary file is fine, or just streaming it across. But requiring the password to be entered puts the kybosh on every way of piping or redirecting the output I can think of. I can probably use a tar pipe to get it across, but it seems there ought to be an easier way to do this.

Actually, the password prompt shouldn't care about redirection, since it goes to the terminal, not stdout specifically.

Ordinary shell redirection should work, I think.

ssh -t -t remotehost sudo cat /path/to/file > /path/to/localfile

Suppose you could try writing it to stderr:

ssh -t -t remotehost 'sudo cat /path/to/file >&2' 2> /path/to/localfile

I cheated a little bit... :slight_smile:

ssh -t $host 'sudo -v'

And then I was able to read my file with sudo but without the password.

1 Like

Be carefull that sudo isn't just using cached credentials, you may find that if you wait 5 mins sudo will require a password (unless you are allowing passwordless sudo for this user/command combo)