gcloud compute execute command remotely

自作多情 提交于 2020-04-07 14:33:48

问题


Currently, if I want to execute something on a VM, I copy files over like this:

gcloud compute --project <project_id> copy-files --zone <zone_name> /home/roman/source/dir roman@<vm_name>:/some/path

Then I need to SSH into it manually like this:

gcloud compute --project <project_id> ssh --zone <zone_name> <vm_name>

And then go and run some command:

cd /some/path
python example.py

How do I combine step 2 and 3 together and execute a command remotely?

Note: I want to use gcloud or the python api. I don't want to use 3rd party packages like Fabric.


回答1:


Try:

$ gcloud compute ssh --zone ZONE INSTANCE -- 'cd /tmp && python some.py'

From gcloud compute ssh --help:

 [-- IMPLEMENTATION-ARGS ...]
    Flags and positionals passed to the underlying ssh implementation.

    The '--' argument must be specified between gcloud specific args on the
    left and IMPLEMENTATION-ARGS on the right. Example:

        $ gcloud compute ssh example-instance --zone us-central1-a -- -vvv \
            -L 80:%INSTANCE%:80



回答2:


A gcloud update that landed a week ago blocked passing ssh commands after '--' and effectively forces use of --command option for this. In this case use:

gcloud compute ssh --zone ZONE INSTANCE --command 'cd /tmp && python some.py'.




回答3:


Did it with a bit of a tangent; using the normal ssh client instead of gcloud compute:

ssh -i ~/.ssh/google_compute_engine roman@<vm_IP> 'python /some/path/example.py'

The gcloud ssh key is located at ~/.ssh/google_compute_engine, which it uses. It also requires the external IP address of the instance instead of its name.



来源:https://stackoverflow.com/questions/39098341/gcloud-compute-execute-command-remotely

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!