【问题标题】:Whats the name of the "initial" GCP cloud shell instance?“初始” GCP 云外壳实例的名称是什么?
【发布时间】:2019-06-30 22:32:51
【问题描述】:

当我登录到 Google 控制台并打开云 shell 时,我有一个 (Bash) shell,并且使用“三个点”菜单,我可以将文件上传到这个实例:

如何使用本地命令行(即从我自己的笔记本电脑上运行的操作系统)执行此操作? 我想我需要运行实例的名称,如何找到?

【问题讨论】:

  • 每次启动 Cloud Shell 时,都会获得不同的 DNS 名称。如果您本周末观看我的网站,我将发布一篇新文章,介绍如何从您的桌面与 Cloud Shell 交互,包括启动连接到 Cloud Shell 的 Putty 的程序的源代码、远程命令执行以及文件上传和下载能力。该程序意味着您可以从桌面向/从 CloudShell 编写命令、文件传输等脚本。 www.jhanley.com 完整的源代码将在 GitHub 上。
  • 源代码发布。用围棋写的。我的文章还有待完成:jhanley.com/google-cloud-shell-cli-in-go
  • 谢谢@JohnHanley,看起来你在那里付出了相当大的努力。所以你以某种方式包装 SDK gcloud 以获得云外壳 VM 的实例名称?
  • 我正在调用 Cloud Shell REST API 以获取有关当前经过身份验证的用户的 Cloud Shell 实例的信息。每个 Google Cloud 用户都有自己的 Cloud Shell 实例。阅读源代码模块github.com/jhanley-com/google-cloud-shell-cli-go/blob/master/…了解更多信息。

标签: google-cloud-platform google-cloud-shell


【解决方案1】:

要获取当前 Cloud Shell 实例的名称,您可以调用 Cloud Shell REST API。此端点将返回描述实例的 JSON:

https://cloud.google.com/shell/docs/reference/rest/v1alpha1/users.environments/get

返回的 JSON 如下所示:

{
  "name": "users/john.smith@example.com/environments/default",
  "id": "default",
  "dockerImage": "gcr.io/cloudshell-images/cloudshell:latest",
  "state": "RUNNING",
  "sshUsername": "john_smith,
  "sshHost": "devshell-vm-5ee6f5d4-3c46-4fee-8865-dc0978b5af98.cloudshell.dev",
  "sshPort": 6000,
  "publicKeys": [
    {
      "name": "users/me/environments/default/publicKeys/8b4afb8d-5fd9-1234-85d9-9527393ea7ca",
      "format": "SSH_RSA",
      "key": "<removed>"
    },
    {
      "name": "users/me/environments/default/publicKeys/0a580367-fd88-56ae-dc4e-5295ee29784e",
      "format": "SSH_RSA",
      "key": "<removed>"
    }
  ]
}

我发布了一个用 Go 编写的完整程序,该程序与 Google Cloud Shell 交互。这包括执行远程命令、上传文件、下载文件和使用 SSH 连接启动 Putty。

Google Cloud Shell CLI in Go

以下示例展示了如何调用 Cloud Shell REST API 来获取实例信息:

package main

import (
    "encoding/json"
    "fmt"
    "os"
    "time"
    "github.com/kirinlabs/HttpRequest"
)

//******************************************************************************************
// Cloud Shell State
//
// https://cloud.google.com/shell/docs/reference/rest/Shared.Types/State
//
// STATE_UNSPECIFIED    The environment's states is unknown.
// DISABLED     The environment is not running and can't be connected to.
//          Starting the environment will transition it to the STARTING state.
// STARTING     The environment is being started but is not yet ready to accept
//          connections.
// RUNNING      The environment is running and ready to accept connections. It
//          will automatically transition back to DISABLED after a period of
//          inactivity or if another environment is started.
//******************************************************************************************

//******************************************************************************************
// https://cloud.google.com/shell/docs/reference/rest/Shared.Types/Environment
//******************************************************************************************

type CloudShellEnv struct {
    Name                 string   `json:"name"`
    Id                   string   `json:"id"`
    DockerImage          string   `json:"dockerImage"`
    State                string   `json:"state"`
    SshUsername          string   `json:"sshUsername"`
    SshHost              string   `json:"sshHost"`
    SshPort              int32   `json:"sshPort"`
    Error struct {
        Code              int32   `json:"code"`
        Message           string  `json:"message"`
        Status            string  `json:"status"`
    } `json:"error"`
}

//******************************************************************************************
// Method: users.environments.get
// https://cloud.google.com/shell/docs/reference/rest/v1alpha1/users.environments/get
//******************************************************************************************

func cloud_shell_get_environment(accessToken string, flag_info bool) (CloudShellEnv, error) {
    //************************************************************
    //
    //************************************************************

    var params CloudShellEnv

    endpoint := "https://cloudshell.googleapis.com/v1alpha1/users/me/environments/default"
    endpoint += "?alt=json"

    req := HttpRequest.NewRequest()

    req.SetHeaders(map[string]string{
            "Authorization": "Bearer " + accessToken,
            "X-Goog-User-Project": config.ProjectId})

    //************************************************************
    //
    //************************************************************

    res, err := req.Get(endpoint)

    if err != nil {
        fmt.Println("Error: ", err)
        return params, err
    }

    body, err := res.Body()

    if err != nil {
        fmt.Println("Error: ", err)
        return params, err
    }

    if flag_info == true {
        fmt.Println("")
        fmt.Println("************************************************************")
        fmt.Println("Cloud Shell Info:")
        fmt.Println(string(body))
        fmt.Println("************************************************************")
    }

    err = json.Unmarshal(body, &params)

    if err != nil {
        fmt.Println("Error: Cannot unmarshal JSON: ", err)
        return params, err
    }

    if params.Error.Code != 0 {
        fmt.Println("")
        fmt.Println(params.Error.Message)
    }

    return params, nil
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-07
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多