I want to copy a file from my Windows host machine to my Google compute instance every few seconds (basically overwrite it). To do this, I want to write a Python script that can run the compute scp command every few seconds. However I cannot seem to find any documentation on how to embed and run the command in a Python script.
Asked
Active
Viewed 477 times
0
-
Hi there! Is it possible for you to add an user to the VM and SCP to the VM using ssh-keys? Or what constraints forces you to use gcloud? – Armando Cuevas Jul 14 '21 at 14:27
-
If you can use ssh-keys, take a look to: https://stackoverflow.com/questions/250283/how-to-scp-in-python – Armando Cuevas Jul 14 '21 at 14:27
-
You do not embed the command in your Python program. The CLI **gcloud** on Windows is a batch (gcloud.cmd) file that launches a Python program. Refer to this question on how to run a batch file from Python: https://stackoverflow.com/questions/5469301/run-a-bat-file-using-python-code – John Hanley Jul 15 '21 at 01:23
1 Answers
0
It's possible to use Python's os.subprocess
command to execute shell commands (including gcloud
) from within Python.
You'll need to ensure that your script access the appropriate ssh keys per the comments.
gcloud compute scp
is a wrapper around the underlying Linux scp
command and it may be better to use scp
directly in this case.
It would be better still to use a Python library|package (see PyPi: scp that enables you to write Python native code that interacts with the scp protocol directly. This approach provides much better control of the process including error handling.

DazWilkin
- 32,823
- 5
- 47
- 88
-
Thanks, this seems to be working. For anyone else who comes to this question, I used paramiko to transfer files to the cloud instance. – notharsh Jul 15 '21 at 13:46