Created
September 23, 2020 04:35
-
-
Save illuzian/710ec6a68b4f37a5a4e341ec29d33da8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from azure.common.credentials import ServicePrincipalCredentials | |
from azure.mgmt.subscription import SubscriptionClient | |
from azure.mgmt.resource import ResourceManagementClient | |
from azure.mgmt.network import NetworkManagementClient | |
import pprint, json | |
pp = pprint.PrettyPrinter(indent=4, depth=40) | |
# Configure the locations to look at | |
locations = ['eastus', 'australiaeast', ] | |
# Create a service principle with the appropriate access and fill in the below | |
tenant = '' | |
client_id = '' | |
secret = '' | |
credentials = ServicePrincipalCredentials( | |
client_id=client_id, | |
secret=secret, | |
tenant=tenant | |
) | |
# Generator for all subscriptions in a tenant. | |
def yield_all(yield_credentials=credentials): | |
subscription_client = SubscriptionClient(yield_credentials) | |
for s in subscription_client.subscriptions.list(): | |
resource_client = ResourceManagementClient(yield_credentials, s.subscription_id) | |
for rg in resource_client.resource_groups.list(): | |
yield {'subscription': s, 'resource_group': rg} | |
def yield_subs(yield_credentials=credentials): | |
subscription_client = SubscriptionClient(yield_credentials) | |
for s in subscription_client.subscriptions.list(): | |
yield s | |
ips = [] | |
ip_dict = {} | |
active_sub_id = '' | |
network_management_client = '' | |
for sub in yield_all(credentials): | |
subscription = sub['subscription'] | |
resource_group = sub['resource_group'] | |
if subscription.subscription_id != active_sub_id: | |
active_sub_id = subscription.subscription_id | |
network_management_client = NetworkManagementClient(credentials, active_sub_id) | |
for network_interface in network_management_client.network_interfaces.list_all(): | |
if 'brick' not in network_interface.id.lower(): | |
if network_interface.name not in ip_dict: | |
ip_dict[network_interface.name] = [] | |
for network_ip_configuration in network_interface.ip_configurations: | |
if network_ip_configuration.private_ip_address not in ip_dict[network_interface.name]: | |
ip_dict[network_interface.name].append(network_ip_configuration.private_ip_address) | |
# Output in a CSV format - handle however you want | |
for interface, addresses in ip_dict.items(): | |
print("{interface}, {ips}".format(interface=interface, ips=", ".join(addresses))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment