4

I'm trying to do the following but it's not quite right.

cat ~/Desktop/blah.png | ssh -t -t [email protected] "cat | sudo -i 'cat | /path/to/blah.png'"

I have my public ssh key in the [email protected] user's .ssh folder, so no initial password is needed. When the sudo -i command finally starts, it prompts me for the root password, but immediately fails as if I had pressed enter but I didn't even touch the keyboard. I have a feeling maybe the cat command is being catted into the password prompt. The output looks like this:

trusktr@LENOVO-PC ~
$ cat ~/Desktop/win-8.1-missing-lockscreen-setting.png | ssh -t -t [email protected] "cat | sudo -i 'cat > /srv/http/default/htdocs/~/img/win-8.1-missing-lockscreen-setting.png'"
tcgetattr: Not a character device
[sudo] password for root:
You type like i drive.
[sudo] password for root:
Your mind just hasn't been the same since the electro-shock, has it?
[sudo] password for root:
Maybe if you used more than just two fingers...
sudo: 3 incorrect password attempts
Connection to 50.116.4.56 closed.

I don't have direct ssh access through the root user, only through a normal user with access to sudo using the root password.

How might I cat the image through ssh, through the non-root user, into the root location all in a single command?

2 Answers 2

1

It does not work because you are cating blah.png into sudo, which is not a valid password.

This should combine the needed commands onto one line:

scp ~/Desktop/blah.png [email protected]:~/ && ssh -t -t [email protected] "sudo cp ~/blah.png /path/to/blah.png && rm ~/blah.png"
7
  • Does it "press enter" when a new line character is catted? How might I achieve what I'm trying to do in a single command?
    – trusktr
    Jan 28, 2014 at 4:28
  • @trusktr, yes, a newline is the same thing as enter, and you don't do it in a single command.
    – psusi
    Jan 28, 2014 at 21:02
  • Thanks for the input. I'm not entirely convinced there isn't a way to do it though.
    – trusktr
    Jan 29, 2014 at 5:16
  • 1
    @trusktr, you can't do it with a single command, but you can combine the commands into a single pipeline using the && operator.
    – psusi
    Jan 29, 2014 at 14:09
  • 1
    Aaah, sweet! That makes sense, and it works here in Git-Bash for Windows (I feared scp might no be available, but it is). Based on your answer, I've also come up with an ssh-only version: cat ~/Desktop/blah.png | ssh [email protected] "cat > ~/blah"; ssh [email protected] "sudo mv ~/blah.png /path/to/blah.png".
    – trusktr
    Feb 1, 2014 at 19:38
1

Option #1: scp the file to your account, then ssh in and put the file in place from the terminal.

Option #2: if your sudo caches auth, login and sudo to root before attempting the complex copy. (the second sudo shouldn't need a password)

(also, sudo uses your password, not root's)

3
  • 1
    Thanks. Yeah, I ended up doing the first option. My setup only caches during the session, so logging out destroys it. sudo can be configured to use the root password instead of the user's password by putting Defaults rootpw in /etc/sudoers.
    – trusktr
    Jan 25, 2014 at 10:41
  • That defeats the purpose of sudo... let people without the rootpw run commands as root.
    – Ricky
    Jan 25, 2014 at 21:31
  • 1
    Depends on the use case. I'm the only user, so it's more secure this way because an adversary now has to guess two passwords instead of just one.
    – trusktr
    Jan 26, 2014 at 8:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .