Skip to content
Repository for my article on Google Cloud Shell CLI in Go
Go Python Batchfile Shell
Branch: master
Clone or download
Latest commit 7fc2e4a Jul 4, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
scripts-linux Improved the README Jul 4, 2019
scripts-windows Added manual authentication for Linux Jul 4, 2019
.gitignore Initial commit Jun 29, 2019
LICENSE
README.md
auth.go Added manual authentication for Linux Jul 4, 2019
cloudshell.go Add ADC support. Improved message and error handling. Merged winssh a… Jul 4, 2019
cmdline.go
config.go Add ADC support. Improved message and error handling. Merged winssh a… Jul 4, 2019
config.json Initial commit Jun 29, 2019
env.go Added partial Linux support Jul 1, 2019
main.go Added manual authentication for Linux Jul 4, 2019
putty.go Add ADC support. Improved message and error handling. Merged winssh a… Jul 4, 2019
sftp.go Add ADC support. Improved message and error handling. Merged winssh a… Jul 4, 2019
ssh.go Add ADC support. Improved message and error handling. Merged winssh a… Jul 4, 2019
webserver.py Added support for Linux desktop terminal window Jul 1, 2019
winssh.go Add ADC support. Improved message and error handling. Merged winssh a… Jul 4, 2019

README.md

google-cloud-shell-cli-go

Repository for my article on Google Cloud Shell CLI in Go

https://www.jhanley.com

This program requires Google OAuth 2.0 Client Credentials. Go to the Google Cloud Console -> APIs & Services. Select Create credentials -> OAuth Client ID. Select "Other" for the Application Type.

Once you have OAuth 2.0 Client Credentials, edit the the file config.json to specify the full path to the credentials file.

The first time you execute this program, you will be prompted to authenticate with Google. These credentials are saved in the file user_credentials.json. In the module auth.go, I show how to store credentials and refresh the access token.

Notes:

  1. This program supports Windows.
  2. This program supports Linux with a Desktop to launch a browser.
  3. This program supports Linux without a Desktop, such as WSL, and supports manual authentication.

I have not ported this program to Mac OS or any other platforms. Volunteers?

Usage: cloudshell [command]
  cloudshell                            - display Cloud Shell information
  cloudshell info                       - display Cloud Shell information
  cloudshell putty                      - connect to Cloud Shell with Putty
  cloudshell ssh                        - connect to Cloud Shell with SSH
  cloudshell exec "command"             - Execute remote command on Cloud Shell
  cloudshell upload src_file dst_file   - Upload local file to Cloud Shell
  cloudshell download src_file dst_file - Download from Cloud Shell to local file

--debug - Turn on debug output
--adc  -  Use Application Default Credentials - Compute Engine only
--auth  - (re)Authenticate ignoring user_credentials.json
--login - Specify an email address as a login hint

Getting Started

This program supports Putty for the SSH client. Download https://www.putty.org/

You will need to setup the SSH keys for Cloud Shell. This requires the "alpha" version of the Google Cloud SDK.

Run these commands in an "Elevated Command Prompt":

  1. Install the alpha components: gcloud components install alpha
  2. Install the beta components: gcloud components install beta
  3. Update the Google Cloud SDK: gcloud components update

Exit the Elevated Command Prompt after updating the Cloud SDK.

Create the SSH key pairs if they do not exist. This command will check and if necessary create the key pairs and install the public key into your Cloud Shell instance.

gcloud alpha cloud-shell ssh --dry-run

Install the Go dependencies:

go get github.com/kirinlabs/HttpRequest
go get github.com/pkg/sftp
go get golang.org/x/crypto/ssh
go get golang.org/x/oauth2/google

Build the program:

go build -o cloudshell.exe

Examples

Run the program and display information about your Google Cloud Shell instance:

cloudshell info

Launch Putty and connect to Cloud Shell:

cloudshell putty

Launch Windows 10 SSH and connect to Cloud Shell:

cloudshell ssh

Launch Linux SSH and connect to Cloud Shell:

cloudshell ssh

Upload a file to Cloud Shell: The local file "local_file.txt" will be copied to the current working directory in Google Cloud Shell.

cloudshell upload local_file.txt remote_file.txt

Quick file copy: This command copies the local file "myfile.txt" to the Cloud Shell default working directory with the same file name.

cloudshell upload myfile.txt

Copy a file to a specic location:

cloudshell upload local_file.txt /tmp/remote_file.txt

What is the current Cloud Shell working directory?

cloudshell exec "pwd"

Display the Cloud Shell current working directory files (directory listing):

cloudshell exec "ls -l"

Note: The remote command must be enclosed in quotation marks

Remote commands that change the environment work but have no effect on the next command. You can combine commands in one session: cloudshell exec "cd /home; cat testfile.txt"

You can’t perform that action at this time.