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:
- This program supports Windows.
- This program supports Linux with a Desktop to launch a browser.
- This program supports Linux without a Desktop, such as WSL, and supports manual authentication.
Usage: cloudshell [command]
cloudshell - display cloudshell program help
cloudshell info - display Cloud Shell information
cloudshell putty - connect to Cloud Shell with Putty
cloudshell ssh - connect to Cloud Shell with SSH
cloudshell winssh - connect to Cloud Shell with Windows OpenSSH
cloudshell winscp - connect to Cloud Shell with Windows WinSCP
cloudshell bitvise - connect to Cloud Shell with Windows Bitvise
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
cloudshell benchmark download - Benchmark download speed from Cloud Shell
cloudshell benchmark upload - Benchmark upload speed from Cloud Shell
--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":
- Install the alpha components:
gcloud components install alpha
- Install the beta components:
gcloud components install beta
- 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"