【问题标题】:How do you SSH into a Google Compute Engine VM instance with Python rather than the CLI?如何使用 Python 而不是 CLI SSH 到 Google Compute Engine VM 实例?
【发布时间】:2020-01-07 09:21:48
【问题描述】:

我想使用google-api-client 通过 SSH 连接到 GCE VM 实例。我可以使用google-api-client 使用以下代码启动一个实例:

from googleapiclient import discovery
from oauth2client.client import GoogleCredentials

credentials = GoogleCredentials.get_application_default()
service = discovery.build('compute', 'v1', credentials=credentials)
project = 'my_project'  
zone = 'us-west2-a'  
instance = 'my_instance' 
request = service.instances().start(project=project, zone=zone, instance=instance)
response = request.execute()

在命令行中,上面的代码呈现为:

gcloud compute instances start my_instance

类似地,使用 SSH 连接到 GCE VM 实例的命令行如下:

gcloud init && gcloud compute ssh my_instance --project my_project --verbosity=debug --zone=us-west2-a

我已经设置了 SSH 密钥等等。

我想知道如何在 Google Api Client 或 Python 中编写上述命令行。

【问题讨论】:

    标签: ssh google-cloud-platform google-compute-engine gcloud google-api-client


    【解决方案1】:

    没有官方的 REST API 方法可以使用 SSH 连接到 Compute Engine 实例。但是假设您按照documentation 配置了SSH 密钥,理论上您可以使用Paramiko 等第三方工具。请查看this 帖子了解更多详情。

    【讨论】:

    • 非常感谢。
    • @kylefoley76 如果答案有帮助,请考虑接受它以扩大知名度并造福社区。​​span>
    • 我不知道您所说的“接受更广泛的知名度”是什么意思。如果您的意思是赞成它,那么我已经这样做了。如果您的意思是在它下面打勾,那么我也已经这样做了。
    猜你喜欢
    • 2017-10-20
    • 2019-04-26
    • 2015-02-16
    • 2020-04-30
    • 1970-01-01
    • 2017-02-05
    • 2014-04-24
    • 2018-07-31
    • 2018-02-25
    相关资源
    最近更新 更多