Script to update site-site vpn hub for every site in one go

Solved
regularadmin
Conversationalist

Script to update site-site vpn hub for every site in one go

Hi All,

 

Does anyone have a script i could use/copy to do this please?

 

Im dreading having to change this 1 by 1!

1 Accepted Solution
Badr-eddine
Getting noticed

Hello, you can use the below script.

 

 

 

 

import os
import meraki
import concurrent.futures
import time

# Create a Meraki API session with API key retrieved from environment variable
API_KEY = os.environ.get('MERAKI_API_KEY')
# simulate : simulate=True or deploy: simulate=False
dashboard = meraki.DashboardAPI(API_KEY, simulate=True)

# Delay between API calls to comply with the rate limit (0.1 seconds (100 milliseconds))
API_CALL_DELAY = 0.1

def update_vpn_hub(network_id, hub_id):
    try:
        # Get the current VPN settings for the network
        vpn_settings = dashboard.appliance.getNetworkApplianceVpnSiteToSiteVpn(network_id)

        if hub_id:
            # Update the VPN hub ID
            vpn_settings['hubs'] = [{'hubId': hub_id, 'useDefaultRoute': True}]

            # Update the VPN settings
            response = dashboard.appliance.updateNetworkApplianceVpnSiteToSiteVpn(
                network_id, vpn_settings['mode'], hubs=vpn_settings['hubs'], subnets=vpn_settings['subnets']
            )

            print(f'Successfully updated VPN hub ID for network ID: {network_id}')
            print(response)
    except meraki.APIError as e:
        print(f'Error updating VPN hub ID for network ID: {network_id}. Error message: {e}')

def update_all_vpn_hubs(org_id, hub_id):
    try:
        # Get a list of networks in the organization
        networks = dashboard.organizations.getOrganizationNetworks(org_id)

        with concurrent.futures.ThreadPoolExecutor() as executor:
            # Iterate over each network and submit update tasks
            futures = []
            for network in networks:
                futures.append(executor.submit(update_vpn_hub, network['id'], hub_id))
                time.sleep(API_CALL_DELAY)  # Introduce a delay between API calls

            # Wait for all tasks to complete
            concurrent.futures.wait(futures)
    except meraki.APIError as e:
        print(f'Error fetching networks. Error message: {e}')

def main():
    # Specify the organization ID
    org_id = 'YOUR ORG ID'

    # Specify the VPN hub name
    hub_name = 'YOUR HUB NAME'

    try:
        # Get a list of networks in the organization
        networks = dashboard.organizations.getOrganizationNetworks(org_id)

        # Find the hub ID by name
        hub_id = next((network['id'] for network in networks if network['name'] == hub_name), None)

        if hub_id:
            update_all_vpn_hubs(org_id, hub_id)
        else:
            print(f'Error: VPN hub name "{hub_name}" not found.')
    except meraki.APIError as e:
        print(f'Error fetching networks. Error message: {e}')

if __name__ == '__main__':
    # Execute the main function
    main()

 

 

 

 

First, add the API key as an environment variable.

 

Create the script file vpn_hub_updater.py in a text editor.

Modify the following variables at the top of the script:

  • org_id: Replace 'YOUR ORG ID' with your actual organization ID.
  • hub_name: Replace 'YOUR HUB NAME' with the desired VPN hub name.

By default, the script is set to simulate the operation (simulate=True). It will only plan the updates without actually making changes to the Meraki networks. If you want to deploy the changes in the production environment, set simulate=False in the dashboard initialization.

Save the changes.

Open a terminal or command prompt and navigate to the directory where the script is located.

Run the script vpn_hub_updater.py.

The script will retrieve the networks in your organization, find the hub ID based on the provided hub name, and update the VPN hub ID for each network. The progress and results will be displayed in the terminal.

Note: The script automatically introduces a delay between API calls to comply with the Meraki API rate limit of 10 calls per second.

Monitor the output in the terminal for any errors or successful updates.

 

Please keep me informed if you encountered any issue!

View solution in original post

2 Replies 2
Badr-eddine
Getting noticed

Hello, you can use the below script.

 

 

 

 

import os
import meraki
import concurrent.futures
import time

# Create a Meraki API session with API key retrieved from environment variable
API_KEY = os.environ.get('MERAKI_API_KEY')
# simulate : simulate=True or deploy: simulate=False
dashboard = meraki.DashboardAPI(API_KEY, simulate=True)

# Delay between API calls to comply with the rate limit (0.1 seconds (100 milliseconds))
API_CALL_DELAY = 0.1

def update_vpn_hub(network_id, hub_id):
    try:
        # Get the current VPN settings for the network
        vpn_settings = dashboard.appliance.getNetworkApplianceVpnSiteToSiteVpn(network_id)

        if hub_id:
            # Update the VPN hub ID
            vpn_settings['hubs'] = [{'hubId': hub_id, 'useDefaultRoute': True}]

            # Update the VPN settings
            response = dashboard.appliance.updateNetworkApplianceVpnSiteToSiteVpn(
                network_id, vpn_settings['mode'], hubs=vpn_settings['hubs'], subnets=vpn_settings['subnets']
            )

            print(f'Successfully updated VPN hub ID for network ID: {network_id}')
            print(response)
    except meraki.APIError as e:
        print(f'Error updating VPN hub ID for network ID: {network_id}. Error message: {e}')

def update_all_vpn_hubs(org_id, hub_id):
    try:
        # Get a list of networks in the organization
        networks = dashboard.organizations.getOrganizationNetworks(org_id)

        with concurrent.futures.ThreadPoolExecutor() as executor:
            # Iterate over each network and submit update tasks
            futures = []
            for network in networks:
                futures.append(executor.submit(update_vpn_hub, network['id'], hub_id))
                time.sleep(API_CALL_DELAY)  # Introduce a delay between API calls

            # Wait for all tasks to complete
            concurrent.futures.wait(futures)
    except meraki.APIError as e:
        print(f'Error fetching networks. Error message: {e}')

def main():
    # Specify the organization ID
    org_id = 'YOUR ORG ID'

    # Specify the VPN hub name
    hub_name = 'YOUR HUB NAME'

    try:
        # Get a list of networks in the organization
        networks = dashboard.organizations.getOrganizationNetworks(org_id)

        # Find the hub ID by name
        hub_id = next((network['id'] for network in networks if network['name'] == hub_name), None)

        if hub_id:
            update_all_vpn_hubs(org_id, hub_id)
        else:
            print(f'Error: VPN hub name "{hub_name}" not found.')
    except meraki.APIError as e:
        print(f'Error fetching networks. Error message: {e}')

if __name__ == '__main__':
    # Execute the main function
    main()

 

 

 

 

First, add the API key as an environment variable.

 

Create the script file vpn_hub_updater.py in a text editor.

Modify the following variables at the top of the script:

  • org_id: Replace 'YOUR ORG ID' with your actual organization ID.
  • hub_name: Replace 'YOUR HUB NAME' with the desired VPN hub name.

By default, the script is set to simulate the operation (simulate=True). It will only plan the updates without actually making changes to the Meraki networks. If you want to deploy the changes in the production environment, set simulate=False in the dashboard initialization.

Save the changes.

Open a terminal or command prompt and navigate to the directory where the script is located.

Run the script vpn_hub_updater.py.

The script will retrieve the networks in your organization, find the hub ID based on the provided hub name, and update the VPN hub ID for each network. The progress and results will be displayed in the terminal.

Note: The script automatically introduces a delay between API calls to comply with the Meraki API rate limit of 10 calls per second.

Monitor the output in the terminal for any errors or successful updates.

 

Please keep me informed if you encountered any issue!

Badr-eddine
Getting noticed

Hello all, 

 

I'm pleased to share an update regarding my script, which now includes support for handling more specific cases. You can access the latest version at the following GitHub repository: https://github.com/baadrdeen/Meraki_vpn_hub_updater.

 

Thank you for your attention and happy exploring!

Get notified when there are additional replies to this discussion.