4

So, I'm trying this:

ssh -t -e none host "sudo cat /location/to/some/file.txt" | xclip -selection c

With -t option, i'm supposed to get password prompt for sudo. But, I'm not. Without it, well permission denied.

What I'm getting for sudo prompt, with -t option, is a new line, where I tried entering the sudo password, but nothing happens and after a little while it drops saying Connection to host closed.

Is there any other way (using terminal only) around to copy a protected file from a remote machine to local.

EDIT:

@Félicien is right. The ssh is masked because of the pipe |. The password-asking-prompt will also go through the pipe and change the input of xclip

  • Does your ssh server forbid root login ? if no, you can connect directly as root ssh -e none root@host "cat /location/to/some/file.txt" | xclip -selection c – Félicien Feb 27 '18 at 11:03
  • yes it does, it does forbid root login – popeye Feb 27 '18 at 11:12
3

While the @Félicien's answer provides the reason of the issue, I found workaround how to solve it by using intermediate temp file and the command tee:

ssh -t -e none -i /path/to/id_rsa user@host "sudo cat /location/to/some/file.txt" | \
    tee /tmp/xclip.txt && \
    sed '1d' /tmp/xclip.txt | \
    xclip -selection c
  • The output of the ssh command will be piped to the command tee that will write it into a new file within /tmp.
  • Then the command sed '1d' will output the content of the new file without the first line that is [sudo] password for <user>: and finally this output ill be piped to xclip.
  • You can create a custom command by adding a function in your .bashrc to make the tings more simple in the future.
  • Isn't tail -2 <file> to print only the 2 last lines of the file? – Félicien Feb 27 '18 at 12:58
  • 1
    I would have used tail -n +2 to ignore the first line – Félicien Feb 27 '18 at 12:59
  • 1
    @Félicien, you are right! I've changed tail with sed. – pa4080 Feb 27 '18 at 13:12
3

From what I tested, the -t option is working, but is masked because of the pipe | in the command. The terminal is waiting for you to put the password but you cannot see it.

You can put your password when the terminal seems to "wait", but the risk is that the password-asking-prompt will also go through the pipe and change the input of xclip

  • What I'm getting for sudo prompt, with -t option, is a new line, where I tried entering the sudo password, but nothing happens and after a little while it drops saying Connection to host closed – popeye Feb 27 '18 at 11:16
  • yes, you're right. I removed the | and it's working fine. But now how do I copy – popeye Feb 27 '18 at 11:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.