Search code examples
pythonshellrsync

sudo rsync on target devices


Good morning,

I am currently having a problem that I cannot find the answer to on StackOverflow or google searches, and I have not yet solved.

I am trying to use rsync as a sudo user on a target device.

The issue:

  • I do not own the target device, so I cannot change ssh/sudo perms.

  • I do not have credentials to the root user

  • I do have credentials to sudo user

  • The transaction must be completed programmatically (minimal user input)

What I've tried:

rsync -a --rsync-path "sudo rsync" USER@HOST:/root/FILE ./

Issue: "A terminal is required to read password"

ok, so let's try passing it through stdin

rsync -a --rsync-path "echo 'PASSWORD' | sudo -S rsync" USER@HOST:/root/FILE ./

rsync: connection unexpectedly closed (0 bytes received so far) [sender]

Issue: rsync error: error in rsync protocol data stream (code 12) at io.c(226) [sender=3.1.3]

rsync: connection unexpectedly closed (4 bytes received so far) [receiver]

rsync error: error in rsync protocol data stream (code 12) at /BuildRoot/Library/Caches/com.apple.xbs/Sources/rsync/rsync-52.200.1/rsync/io.c(453) [receiver=2.6.9]

Do you guys have any other ideas about what I could be doing?

I am aware that echoing the password is not best practice, however I do not have many other options in the case that the server I am connecting to has not done a key exchange with root user and I cannot change the SUDOPASS settings.

In the end this is all getting plugged into a Python script, so if there is a better pythonic means of using rsync as a sudoer, please inform me.


Solution

  • If your remote sudo is configured so that once you have given the password, you do not need to give it again for a while, then you can try this:

    rsync -a --rsync-path "echo 'PASSWORD' | sudo -S date >&/dev/null; sudo rsync" \
      USER@HOST:/root/FILE ./
    

    To debug what command is being run on the remote add --debug=CMD2. If your remote does not understand the bash syntax >&/dev/null, use the longer >/dev/null 2>/dev/null.