-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_instances_with_eips.py
More file actions
25 lines (20 loc) · 906 Bytes
/
get_instances_with_eips.py
File metadata and controls
25 lines (20 loc) · 906 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import argparse
from google.cloud import compute_v1
def get_gce_with_eips(project_id):
""" Prints all the instances with external IP in any of its network interface """
instance_client = compute_v1.InstancesClient()
agg_list = instance_client.aggregated_list(project=project_id)
for zone, response in agg_list:
if response.instances:
for instance in response.instances:
for interface in instance.network_interfaces:
if interface.access_configs:
print(f"Instance Name: {instance.name}, Instance ID: {instance.id}, Instance Zone: {zone}")
break
def main():
parser = argparse.ArgumentParser()
parser.add_argument("project_id", help="Your Google Cloud project ID.")
args = parser.parse_args()
get_gce_with_eips(args.project_id)
if __name__ == "__main__":
main()