Skip to content

Instantly share code, notes, and snippets.

@rajathithan
Last active April 17, 2023 10:15
Show Gist options
  • Save rajathithan/3cd858198cafef22f684a9cb42ad4dcd to your computer and use it in GitHub Desktop.
Save rajathithan/3cd858198cafef22f684a9cb42ad4dcd to your computer and use it in GitHub Desktop.
compute-engine-details
#!/bin/bash
# Script to retrieve compute engine details.
# Author - Rajathithan Rajasekar - 03/03/2020
echo "PROJECT NAME, INSTANCE NAME , ZONE , MACHINE-TYPE , OPERATING SYSTEM , CPU , MEMORY , DISK SIZE" > compute-engine-details.csv
prjs=( $(gcloud projects list | tail -n +2 | awk {'print $1'}) )
for i in "${prjs[@]}"
do
echo "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@" >> list.txt
echo "Setting Project: $i" >> list.txt
echo $(gcloud config set project $i)
if [[ $(gcloud services list --enabled --filter="NAME:compute.googleapis.com" --format="value(NAME)" | grep compute) ]]; then
echo "Compute API is enabled is in project - $i"
echo $(gcloud compute instances list | awk '{print $1,$2}' | tail -n +2| while read line; do echo "$i $line"; done |xargs -n3 sh -c 'python3 retrieve-compute-engine-details.py $1 $2 $3 >> compute-engine-details.csv' sh)
else
echo "Compute API is not enabled in project - $i"
fi
done
@Di3g01314
Copy link

Hi, we can guide ourselves to see how we can get the IP?

@rajathithan
Copy link
Author

Hi,

I hope you are running it from cloud shell, there is a bug with gcloud SDK to display the output in rows instead of columnar format. update your gcloud sdk in cloud shell and rerun the script .

Thanks,
Raj

@VishnuvardhanKrishnan
Copy link

VishnuvardhanKrishnan commented Nov 2, 2022

@rajathithan - While trying to execute the script, I'm getting the below error. I'm running it on the compute engine. Any thoughts?

Updated property [core/project].

Traceback (most recent call last):
  File "retrieve-compute-engine-details.py", line 23, in <module>
    response = request.execute()
  File "/home/vishnu/.local/lib/python3.8/site-packages/googleapiclient/_helpers.py", line 130, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/home/vishnu/.local/lib/python3.8/site-packages/googleapiclient/http.py", line 938, in execute
    raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 403 when requesting https://compute.googleapis.com/compute/v1/projects/sample-project/zones/us-east4-a/instances/apache-test?alt=json returned "Re
quired 'compute.instances.get' permission for 'projects/sample-project/zones/us-east4-a/instances/apache-test'". Details: "[{'message': "Required 'compute.instances.get' permission for 'proje
cts/sample-project/zones/us-east4-a/instances/apache-test'", 'domain': 'global', 'reason': 'forbidden'}]">
Traceback (most recent call last):
  File "retrieve-compute-engine-details.py", line 23, in <module>
    response = request.execute()
  File "/home/vishnu/.local/lib/python3.8/site-packages/googleapiclient/_helpers.py", line 130, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/home/vishnu/.local/lib/python3.8/site-packages/googleapiclient/http.py", line 938, in execute
    raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 403 when requesting https://compute.googleapis.com/compute/v1/projects/sample-project/zones/us-east4-b/instances/apache-php?alt=json returned "Requi
red 'compute.instances.get' permission for 'projects/sample-project/zones/us-east4-b/instances/apache-php'". Details: "[{'message': "Required 'compute.instances.get' permission for 'projects/gs
k-ch-apache-nonprod-d43f/zones/us-east4-b/instances/apache-php'", 'domain': 'global', 'reason': 'forbidden'}]">
Traceback (most recent call last):
  File "retrieve-compute-engine-details.py", line 23, in <module>
    response = request.execute()
  File "/home/vishnu/.local/lib/python3.8/site-packages/googleapiclient/_helpers.py", line 130, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/home/vishnu/.local/lib/python3.8/site-packages/googleapiclient/http.py", line 938, in execute
    raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 403 when requesting https://compute.googleapis.com/compute/v1/projects/sample-project/zones/us-east4-c/instances/apache-php-1?alt=json returned "Requi
red 'compute.instances.get' permission for 'projects/sample-project/zones/us-east4-c/instances/apache-php-1'". Details: "[{'message': "Required 'compute.instances.get' permission for 'projects/gs
k-ch-apache-nonprod-d43f/zones/us-east4-c/instances/apache-php-1'", 'domain': 'global', 'reason': 'forbidden'}]">
Traceback (most recent call last):
  File "retrieve-compute-engine-details.py", line 23, in <module>
    response = request.execute()
  File "/home/vishnu/.local/lib/python3.8/site-packages/googleapiclient/_helpers.py", line 130, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/home/vishnu/.local/lib/python3.8/site-packages/googleapiclient/http.py", line 938, in execute
    raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 403 when requesting https://compute.googleapis.com/compute/v1/projects/sample-project/zones/us-east4-c/instances/apache-php-2?alt=json returned "Re
quired 'compute.instances.get' permission for 'projects/sample-project/zones/us-east4-c/instances/apache-php-2'". Details: "[{'message': "Required 'compute.instances.get' permission for 'proje
cts/sample-project/zones/us-east4-c/instances/apache-php-2'", 'domain': 'global', 'reason': 'forbidden'}]">

@rajathithan
Copy link
Author

403 is a forbidden error, you dont have access to that project.

@danielterriquez
Copy link

@rajathithan While trying to execute the script, I'm getting the below error. ModuleNotFoundError: No module named 'googleapiclient'. Any thoughts?

test@ubuntu:~/Downloads$ ./compute-engine-details.sh
Updated property [core/project].

Compute API is enabled is in project - ate-bastion
Traceback (most recent call last):
File "retrieve-compute-engine-details.py", line 4, in
from googleapiclient import discovery
ModuleNotFoundError: No module named 'googleapiclient'

Updated property [core/project].

image

@danielterriquez
Copy link

@rajathithan While trying to execute the script, I'm getting the below error. credentials = GoogleCredentials.get_application_default(). Any thoughts?

root@ubuntu:/home/test/Downloads# ./compute-engine-details.sh
Updated property [core/project].

Compute API is enabled is in project - ate-bastion
Traceback (most recent call last):
File "retrieve-compute-engine-details.py", line 10, in
credentials = GoogleCredentials.get_application_default()
File "/usr/local/lib/python3.8/dist-packages/oauth2client/client.py", line 1271, in get_application_default
return GoogleCredentials._get_implicit_credentials()
File "/usr/local/lib/python3.8/dist-packages/oauth2client/client.py", line 1261, in _get_implicit_credentials
raise ApplicationDefaultCredentialsError(ADC_HELP_MSG)
oauth2client.client.ApplicationDefaultCredentialsError: The Application Default Credentials are not available. They are available if running in Google Compute Engine. Otherwise, the environment variable GOOGLE_APPLICATION_CREDENTIALS must be defined pointing to a file defining the credentials. See https://developers.google.com/accounts/docs/application-default-credentials for more information.

@VishnuvardhanKrishnan
Copy link

VishnuvardhanKrishnan commented Dec 14, 2022

Hi,

I hope you are running it from cloud shell, there is a bug with gcloud SDK to display the output in rows instead of columnar format. update your gcloud sdk in cloud shell and rerun the script .

Thanks, Raj

@rajathithan Thank you. Can you let me know how to get the hostname and Private IP of the machines through this script (both Linux & Windows)?

Thanks, Vishnu

@ricguarnieri
Copy link

While trying to execute the script, I'm getting the below:

  File "/home/xpto/compute_details/retrieve-compute-engine-details.py", line 19, in <module>
    request = service.instances().get(project=project, zone=zone, instance=instance)
  File "/usr/local/lib/python3.9/dist-packages/googleapiclient/discovery.py", line 1073, in method
    raise TypeError(
TypeError: Parameter "zone" value "NAME:" does not match the pattern "[a-z](?:[-a-z0-9]{0,61}[a-z0-9])?"
Traceback (most recent call last):
  File "/home/xpto/compute_details/retrieve-compute-engine-details.py", line 19, in <module>
    request = service.instances().get(project=project, zone=zone, instance=instance)
  File "/usr/local/lib/python3.9/dist-packages/googleapiclient/discovery.py", line 1073, in method

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment