I'm trying to write a Python string (yaml
) to /path/to/foo.yaml
on a remote linux machine.
There's some fiddly Python/bash interop here.
My current solution looks like this:
ip = '1.2.3.4'
yaml = '...'
src = f'tmp-{i}.yaml'
dst = f'root@{ip}:/path/to/foo.yaml'
with open(src, 'w') as f:
f.write(yaml)
cmd = f'scp -o StrictHostKeyChecking=no -i mykey.pem {src} {dst}'
subprocess.run(cmd.split())
So I'm writing to a temp-file, and then scp
-ing that temp-file across using bash-in-Python.
Is there a more compact solution that avoids the temp-file creation?
WARNING: My solution is likely not resilient against filenames with spaces, so do not use blindly. If you are dealing with filenames with spaces, consider not doing that.
WARNING: google StrictHostKeyChecking
if you use this code for anything that requires security