Discussion:
remote scp command
(too old to reply)
Jurgen Lamsens
2009-09-25 13:00:01 UTC
Permalink
Hi,

I hope I'm at the right place for my question. Consider this:

***@ubuntu1:~$ cat /etc/hosts
127.0.0.1 localhost
192.168.155.186 ubuntu1
192.168.155.187 ubuntu2

***@ubuntu1:~$ touch file.txt

1.) This works, because I can write to /tmp
***@ubuntu1:~$ scp file.txt ***@192.168.155.187:/tmp
***@192.168.155.187's password:
file.txt
100% 0 0.0KB/s 00:00

2.) This works, because I use sudo
***@ubuntu1:~$ ssh ***@192.168.155.187 'sudo touch /root/file.txt'
***@192.168.155.187's password:

3.) I want to scp to a directory that I do not have access to, but I
cannot give some kind of sudo parameter to scp:
***@ubuntu1:~$ scp file.txt ***@192.168.155.187:/root/
***@192.168.155.187's password:
scp: /root//file.txt: Permission denied

How can I make the last one work in one shot, knowing that I can use
sudo in step 2.)
-> I don't want to login to ubuntu2, and do the scp the other way arount
-> I don't want to scp to e.g. ubuntu2:/tmp first, login to ubunt2 and
move from ubuntu2:/tmp to ubuntu2:/root

Thanks in advance,
Kind regards,
Jurgen Lamsens
Greg Wooledge
2009-09-28 16:19:29 UTC
Permalink
Post by Jurgen Lamsens
3.) I want to scp to a directory that I do not have access to, but I
scp: /root//file.txt: Permission denied
The easy way would be:

scp file.txt ***@192.168.155.187:/root/

The harder way would be something like:

ssh ***@192.168.155.187 sudo sh -c '"cat > /root/file.txt"' < file.txt

But this requires a no-password-given sudoer permission on 192.168.155.187
because even if you allocate a pseudo-terminal with "ssh -t", you're
still using the stdin stream for data, so sudo can't use it to ask you
for your password.
Males, Jess
2009-09-28 18:23:15 UTC
Permalink
Jurgen,

Thanks for asking. Initially, I wanted to say, "not possible," but then I =
considered the form of your question and realized that there was a way.

date | ssh localhost 'sudo sh -c "/bin/cat > /etc/tmp"'

The core of this is that you can pipe into ssh and it'll connect it through=
the session. I'd seen this in examples for tarring files across an ssh se=
ssion for file transfers; never really used it.

The nasty bit is, as I have it, I had to add sh to the sudo file. Ok, nast=
y is an understatement, this is detestable; don't do it. For your solution=
, you'll probably want to write a little script that just does, "cat > $fil=
e", and add the script to /etc/sudoers.

The input redirection isn't interpreted as part of the sudo command; it's s=
till the local account, thus the small script or sh -c wrapper. If there's=
a better way around this, please share.

I also had issue with typing the password for the sudo, so I just used nopa=
sswd for the test.

so, your file transfer should be something like (not tested):

cat /path/to/source | ssh host "sudo dump.sh"=20

dump.sh:
#!/bin/sh
cat > $1




-- Jess Males


-----Original Message-----
From: ***@securityfocus.com [mailto:***@securityfocus.com] On=
Behalf Of Jurgen Lamsens
Sent: Friday, September 25, 2009 6:52 AM
To: ***@securityfocus.com
Subject: remote scp command

Hi,

I hope I'm at the right place for my question. Consider this:

***@ubuntu1:~$ cat /etc/hosts
127.0.0.1 localhost
192.168.155.186 ubuntu1
192.168.155.187 ubuntu2

***@ubuntu1:~$ touch file.txt

1.) This works, because I can write to /tmp
***@ubuntu1:~$ scp file.txt ***@192.168.155.187:/tmp
***@192.168.155.187's password:
file.txt =
=20
100% 0 0.0KB/s 00:00 =20

2.) This works, because I use sudo
***@ubuntu1:~$ ssh ***@192.168.155.187 'sudo touch /root/file.txt=
'
***@192.168.155.187's password:

3.) I want to scp to a directory that I do not have access to, but I=20
cannot give some kind of sudo parameter to scp:
***@ubuntu1:~$ scp file.txt ***@192.168.155.187:/root/
***@192.168.155.187's password:
scp: /root//file.txt: Permission denied

How can I make the last one work in one shot, knowing that I can use=20
sudo in step 2.)=20
-> I don't want to login to ubuntu2, and do the scp the other way arount
-> I don't want to scp to e.g. ubuntu2:/tmp first, login to ubunt2 and=20
move from ubuntu2:/tmp to ubuntu2:/root

Thanks in advance,
Kind regards,
Jurgen Lamsens

Loading...