logic1976
logic1976

Reputation: 581

How do you SSH into a Google Compute Engine VM instance with Python rather than the CLI?

I want to SSH into a GCE VM instance using the google-api-client. I am able to start an instance using google-api-client with the following code:

from googleapiclient import discovery
from oauth2client.client import GoogleCredentials

credentials = GoogleCredentials.get_application_default()
service = discovery.build('compute', 'v1', credentials=credentials)
project = 'my_project'  
zone = 'us-west2-a'  
instance = 'my_instance' 
request = service.instances().start(project=project, zone=zone, instance=instance)
response = request.execute()

In the command line the above code is rendered as:

gcloud compute instances start my_instance

Similarly, to SSH into a GCE VM instance with the command line one writes:

gcloud init && gcloud compute ssh my_instance --project my_project --verbosity=debug --zone=us-west2-a

I've already got the SSH keys set up and all that.

I want to know how to write the above command line in Google Api Client or Python.

Upvotes: 0

Views: 2888

Answers (1)

Maxim
Maxim

Reputation: 4431

There is no official REST API method to connect to a Compute Engine instance with SSH. But assuming you have the SSH keys configured as per the documentation, in theory, you could use a third-party tool such as Paramiko. Take a look at this post for more details.

Upvotes: 1

Related Questions