Trigger Device Sync for Specific Devices

Scenario Overview

This guide explains how network administrators can sync device configurations with the CNC server using device IPs.

Prerequisites

Before running, obtain an access token (see 'Getting Started'), configure cnc-api-common.sh, and verify the input/output directories exist.

Trigger Device Sync for Specific Devices

Update the input_device_address array in the device_sync.sh script with the IP address(es) of the devices you want to sync. Then, run the script from the example directory as shown below.

cd cnc-inventory-api-examples;./device_sync.sh

Script Details

#!/bin/bash
. ./cnc-api-common.sh

device_sync() {
    # Assuming the CNC JWT has already been obtained by running the get-cnc-jwt.sh script.
    # Read from JWT file and export it as AUTH_TOKEN_HDR
     export_jwt

    # STEP 1 : Retrieve all the nodeuuids on the server
    # STEP 2 : Retrieve nodeuuids for the device IPs provided by the user below
    # STEP 3 : Trigger sync for the provided devices

    NODEUUID_OUTPUT="$PRJ/output/nodeuuid.json"
    get_nodeuuids $NODEUUID_OUTPUT
    # Replace the input_device_address array with one or more IP addresses of the devices that you want to sync. For multiple devices, provide the IPs as an array, where each IP is enclosed in double quotes and separated by spaces.
    input_device_address=("0.0.0.0" "255.255.255.255")  
    node_uuids=""

    for ip in "${input_device_address[@]}"; do
      # Use jq to find the corresponding nodeUUId
      node_uuid=$(jq -r --arg address "$ip" '.[] | select(.deviceAddress == $address) | .nodeUUId' $NODEUUID_OUTPUT)

      # Check if a nodeUUId was found
      if [ -n "$node_uuid" ]; then
        node_uuids+="\"$node_uuid\","
        echo "The nodeUUId for device address $input_device_address is $node_uuid."
      else
        echo "No nodeUUId found for device address $input_device_address."
      fi
    done
    node_uuids=${node_uuids%,}

    CNC_API_URL=$CNC_INVENTORY_API_CTX/sync
    CNC_API_INPUT="$PRJ/input/device_sync_input.json"	
    echo "{\"nodeuuids\": [$node_uuids], \"allnodes\": \"false\"}" > $CNC_API_INPUT
    CNC_API_OUTPUT="$PRJ/output/device_sync_output.json"
    http_post $CNC_API_URL @$CNC_API_INPUT $CNC_API_OUTPUT
}
device_sync