To get the name of the current Cloud Shell instance you can call the Cloud Shell REST API. This endpoint will return JSON describing the instance:
https://cloud.google.com/shell/docs/reference/rest/v1alpha1/users.environments/get
The return JSON looks like this:
{
"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>"
}
]
}
I have published a full program written in Go that interfaces with Google Cloud Shell. This includes executing remote commands, uploading files, download files and launching Putty with an SSH connection.
Google Cloud Shell CLI in Go
The following example shows how to call the Cloud Shell REST API to get the instance information:
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, ¶ms)
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
}