'Cannot use gcloud compute ssh command in python subprocess

I have a compute engine already set up, I can use ssh command in command prompt, like

gcloud compute ssh test-instance@cloud-engine-noapi --zone us-central1-f --command "rm -f test.txt"

and successfully delete the test.txt in server.

However, when I call these in python.

subprocess.call(['gcloud', 'compute', '--project', 'test-project','ssh', temp_instance, '--zone', zone, '--command', '"cd /home"'], shell=True)
subprocess.call(['gcloud', 'compute', '--project', 'test-project','ssh', temp_instance, '--zone', zone, '--command', '"ls -l"'], shell=True)
subprocess.call(['gcloud', 'compute', '--project', 'test-project','ssh', temp_instance, '--zone', zone, '--command', '"rm -f /home/test.txt"'], shell=True)

The return is always like

bash: /command : No such file or directory

and for command like ls

bash: /command : command not found

Is there any process I have to do first?



Solution 1:[1]

I know that this is quite old but it took me some time to figure it out. In my case helped avoiding double quotes (despite it's recommended by gcloud cli help).

Example:

subprocess.call(['gcloud', 'compute', f'--project={test-project}','ssh', temp_instance, f'--zone={zone}, '--command=cd /home'], shell=True)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Leszek Bulawa