1

I would like to use rlcone inside python. If I don't encrypt my rclone config with a password, then I can simply do this:

import subprocess 
process = subprocess.Popen(["rclone", "listremotes"], shell=True, stdin=subprocess.PIPE)
output = process.communicate()
print(output)

But I want to protect my Rclone config with a password, so I need a way to send it to rclone. I followed this answer but I get the error Failed to read password: The handle is invalid:

import subprocess 
psw= input("enter psw")
psw_byte= str.encode(psw+'\n')
process = subprocess.Popen(["rclone", "listremotes"], shell=True, stdin=subprocess.PIPE,stdout=subprocess.PIPE, stderr=subprocess.PIPE)
process.stdin.write(psw_byte)
process.stdin.flush()
output = process.communicate()
print(output)
MagTun
  • 5,619
  • 5
  • 63
  • 104
  • By the by, the `shell=True` is superfluous here. You neither need nor want a shell in order to run a single command. – tripleee Mar 31 '18 at 14:10
  • 1
    It seems `rclone` doesn't read its password from standard input. Many programs force a read from `/dev/tty` or some such. Maybe there is a different way to pass in a password when running scripted. – tripleee Mar 31 '18 at 14:12
  • Looking at the documentation, maybe you can set the environment variable `RCLONE_CONFIG_PASS` with the password when calling the subprocess. – JohanL Mar 31 '18 at 15:52

4 Answers4

3

I recommend you use the RCLONE_CONFIG_PASS environment variable to pass the config password into rclone. See the section in the docs about configuration encryption.

Eg

os.environ("RCLONE_CONFIG_PASS") = "mypassword"

Unix programs reading passwords tend not to be scriptable like this unless you assign a tty.

That is quite a lot of trouble though, so I recommend just setting the environment variable instead.

PS If you want to just set the enviroment for the subprocess you can do it like this: Python subprocess/Popen with a modified environment

PPS I'm the author of rclone :-)

Nick Craig-Wood
  • 52,955
  • 12
  • 126
  • 132
  • Apparently using modified environment isn't a safe way to pass the password: https://stackoverflow.com/questions/53232728. As rclone doesn't read its password from standard input, is there a secure way to pass the password to rlcone via python? – MagTun Nov 10 '18 at 19:13
  • As of recent `rclone` versions, it is better to use `RCLONE_PASSWORD_COMMAND` with a secrets management system like `gnome-keyring`, e.g. `export RCLONE_PASSWORD_COMMAND='secret-tool lookup service "rclone" user "calisync"'` via https://rclone.org/docs/#configuration-encryption – HaoZeke Aug 19 '23 at 14:14
0

This may not address the question exactly, yet, as I have not found this in this form elsewhere, here's a solution to use in ipython and/or jupyter ipynb.

Can be adapted to the meet the question:

import getpass
rc_passwd = getpass.getpass()

! rclone --password-command "echo {rc_passwd}" lsd rc_remote:rc_path

for rc_path being the path in rc_remote

eldad-a
  • 3,051
  • 3
  • 22
  • 25
0

I suggest to use

rclone obscure 'thepass'

and to see or use config file

rclone config file

insert it instead of pass or possible to use new conf file by using

   rclone ... --config=
Zaman
  • 811
  • 7
  • 15
-1

One way around is to use subprocess.call() and to call a .cmd into which you store the cmd command:

in python:

import subprocess
cmd_path = "C:\Users\user\Desktop\rclone_command.cmd"
subprocess.call([cmd_path]) 

in rclone_command.cmd (after rclone is done sync, you can start another python script cf start ex to parse the log):

rclone sync  "C:\my path" "remote":"folder" --log-file "C:\Users\user\Desktop\log.log"  --log-level INFO --delete-after   --exclude "desktop.ini" 

start "" "C:\Users\user\Desktop\some other script.py"
MagTun
  • 5,619
  • 5
  • 63
  • 104