连接从Python脚本创建的实例

发布于 2025-02-01 01:51:03 字数 728 浏览 4 评论 0原文

我想自动创建客户端实例并在其上运行客户端python脚本。我可以创建一个客户端实例:

from googleapiclient import discovery
from oauth2client.client import GoogleCredentials

credentials = GoogleCredentials.get_application_default()
service = discovery.build('compute', 'beta', credentials=credentials)

project = 'my-project'
zone = 'us-central1-a'

instance_body = {
    "name": "my-instance-name",
    "sourceMachineImage": "projects/my-project/global/machineImages/my-image",
}

request = service.instances().insert(project=project, zone=zone, body=instance_body)
response = request.execute()

但是,响应对象似乎不包含新实例的任何身份验证详细信息。由于我有实例的名称,因此我可以从实例列表中获取其内部IP地址,但这还不足以连接到它。那么如何在新创建的实例上运行我的客户端Python脚本呢?

I would like to automatically create client instances and run client Python script on them. I can create a client instance as follows:

from googleapiclient import discovery
from oauth2client.client import GoogleCredentials

credentials = GoogleCredentials.get_application_default()
service = discovery.build('compute', 'beta', credentials=credentials)

project = 'my-project'
zone = 'us-central1-a'

instance_body = {
    "name": "my-instance-name",
    "sourceMachineImage": "projects/my-project/global/machineImages/my-image",
}

request = service.instances().insert(project=project, zone=zone, body=instance_body)
response = request.execute()

However, the response object does not seem to contain any authentication details for the new instance. Since I have the name of the instance, I can obtain its internal IP address from the list of instances, but that is not enough to connect to it. So how can run my client Python script on the newly created instance?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

撩人痒 2025-02-08 01:51:03

Google的Compute Engine API用于为Google Cloud Platform编程“控制平面”,即它用于在Google Cloud Platform上创建,更新和删除计算引擎资源。

创建计算引擎实例(VM)后,您需要使用操作特定的工具远程访问操作系统。例如,如果您要创建Linux实例,则可以使用EG ssh 与VM进行交互。对于Windows,我认为该工具称为 rdp

Google提供凭证(适用于操作系统的所有口味)供您访问VM。使用Linux VM时,一种常见的方法是使用SSH和SSH键。如果您使用gcloud Compute ssh连接到一个实例,则gcloud将为您使用本地生成的SSH键(Google> Google_compute_engine)并自动化该过程。对于Windows,有一个不同的过程。

因此,您将需要:

  1. 运行上述代码以创建实例返回,例如IP | DNS
  2. 创建或重用凭据,以适当访问VM
  3. 使用(或使用a
  4. /moin/secureshell“ rel =“ nofollow noreferrer”> python库)ssh或rdp,可以使用凭据
  5. 运行脚本

Google's Compute Engine API is used to program the "control plane" for Google Cloud Platform, i.e. it's used to create, update and delete Compute Engine resources on Google Cloud Platform.

Once you've created a Compute Engine instance (VM), you need to use operating-specific tools to access the operating system remotely. For example, if you're creating Linux instances then you can interact with the VM using e.g. SSH. For Windows, I think the tool is called RDP.

Google provides credentials (for all flavors of operating system) for you to access the VM. A common approach when using Linux VMs, is to use SSH and SSH keys. If you use gcloud compute ssh to connect to an instance, gcloud will use locally-generated SSH keys for you (google_compute_engine) and automate the process. For Windows there's a different process.

So you will need to:

  1. Run the above code to create the instance returning e.g. IP|DNS
  2. Create or reuse credentials that provide suitable access to the VM
  3. Use (or automate using a Python library) SSH or RDP to connect to the VM
  4. Authenticate using the credentials
  5. Run your script
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文