Triggering NetBrain to Discover or Remove Devices

An enterprise usually deploys a configuration management database (CMDB) tool (such as HP NA) to maintain an accurate repository of network devices. NetBrain supports to integrate with these CMDB tools to ensure the data accuracy in NetBrain domains. That is, whenever new devices are added into or removed from a CMDB, the CMDB can trigger NetBrain to either discover or remove the devices.

Discovering Devices via APIs

Removing Devices via APIs

Discovering Devices via APIs

This section covers the use case to discover new devices via APIs.

Workflow

The pseudo-code below describes the main flow in the API script to discover devices to NetBrain.

1. Login to NetBrain API server and get session
2. Set the current domain
3. Call GetAllDiscoveryTasks and pick up one task
4. For each newly added device
    i. Call AddIPforDiscovery to add its IP and/or credentials into the task
5. Call GetAllIPsForDiscovery and confirm that all IPs are added successfully
6. Call RunDiscoveryNow to run the discovery task
7. Keep calling GetDiscoveryTaskStatus each one minute till the task is completed or completed with error
8. Call GetDiscoveryTaskResult to get all IPs the task has tried to discover
9. For each IP in results
    i. If (ping failed) print ping failed
    ii. If (Telnet/SSH status failed)
        a. Call GetLiveAccessLog and print out the live access log
10. Logout to NetBrain API server

API Script 

You can refer to the script below to call relative APIs. This sample script first imports a python library maintained by NetBrain and you need to write your own one in practical use.

import netbrain_restful_lib
import time
  
user = "NetBrain"       
pwd = "Netbrain"       
nb_url = "http(s)://<IP address of NetBrain Web Server>/"            
headers = {'Content-Type''application/json''Accept''application/json'}
tenantName = "Initial Tenant"
domainName = "Use Case"
  
#################################################################
  
# Step 1: Login to NetBrain API server and get session
token = netbrain_restful_lib.loginSession(nb_url, user, pwd)
tenantList = netbrain_restful_lib.getTenants(nb_url, token)
domainList = netbrain_restful_lib.getDomains(nb_url, token)
#Sample results
# token : adf5b560-8e9c-4114-87bb-2eed24f0faff
# tenantList : [{'tenantId': 'a50af282-fa02-012f-a7f7-e410cc7c29d3', 'tenantName': 'Initial Tenant'}]
# domainList : [{'domainName': 'UseCase', 'domainId': '4f747a8b-71da-404b-a05c-e8505aa14725'}]
   
tenantId = 'fcf8a380-6b11-0fed-1d51-3b4cdef8f238'
domainId = '25b66365-1ab8-4fed-90c9-1307a4047c3d'
   
 
# Step 2: Set current domain
print(netbrain_restful_lib.loginDomain(nb_url, token, tenantId, domainId))
# Sample results
# loginDomain: Login successfully!
   
 
# Step 3: Call GetAllDiscoveryTasks and pick up one task
print(netbrain_restful_lib.getDiscoveryTasks(nb_url, token))
taskId = "1696faa7-749c-78a7-eaad-fcf815b11e4a"
# Sample results
# GetAllDiscoveryTasks : [{"id": "1696faa7-749c-78a7-eaad-fcf815b11e4a", "name": "Scheduled System Discovery", "enable": true, "lastStatus": "", "lastRunSpan": "00:00:00", "curStatus": "Not Started", "nextRunTime": "1/2/3000,12:00:00 AM", "frequency": "Once"}]
 
 
# Step 4: For each newly added device, call AddIPforDiscovery to add its IP and/or credentials into the task
data=[{"IP":"192.168.0.1""cliType":0} ]
for i in data:
    print(netbrain_restful_lib.postIPstoDiscoveryTask(nb_url, token, taskId, i['IP'], i["cliType"]))
# Sample results
# postIPstoDiscoveryTask : Success
 
 
# Step 5: Call GetAllIPsForDiscovery and confirm that all IPs are added successfully
print(netbrain_restful_lib.getIPinDiscoveryTask(nb_url, token, taskId))
# Sample results
# getIPinDiscoveryTask: ["192.168.0.1"]
 
 
# Step 6: Call RunDiscoveryNow to run the discovery task
print(netbrain_restful_lib.runDiscoveryTask(nb_url, token, taskId))
# Sample results
# runDiscoveryTask: Success
 
 
# Step 7: Keep calling GetDiscoveryTaskStatus each one minute till the task is completed or completed with error
print(netbrain_restful_lib.getDiscoveryRunningStatus(nb_url, token, taskId))
# Sample results
# getDiscoveryRunningStatus: 10
#Possible values-1 Unknown, 0 Never run, 2 Running, 10 Succeeded, 11 Succeeded with warning, 20 Failed, 30 Manually stopped, 31 Automatically stopped due to timeout set by user or other system setting
 
 
# Step 8: Call GetDiscoveryTaskResult to get all IPs the task has tried to discover
print(netbrain_restful_lib.getDiscoveryResults(nb_url, token, taskId))
# Sample results
# getDiscoveryResults: [{"domainId": "Domain","source": "Scan 172.24.101.12/32","hostname": "Bos-Core1", "frontServer": "10.10.1.43", "ping": "Succeeded", "SNMP": "nb", "vendor": "Cisco", "oid": "1.3.6.1.4.1.9.1.324","type": "Cisco IOS Switch", "config": "Succeeded", "telnetSSH": "Succeeded" } ]
 
 
# Step 9: For each IP in results, check if results failed. If (Telnet/SSH status failed), call GetLiveAccessLog and print out the live access log
print(netbrain_restful_lib.getDiscoveryLiveAccessLog(nb_url, token, taskId))
# Sample results
# getDiscoveryLiveAccessLog: [{"mgmtIP": "172.24.101.12", "liveLogs": "Ping [172.24.101.12] via Front Server(10.10.1.43); Succeeded\r\nSend RO = [nb][version:v2c] to [172.24.101.12] via Proxy Server(10.10.1.43); Succeeded\r\nRetrieving [172.24.101.12]'s Hostname ,Vendor and Model via Proxy Server(10.10.1.43); Succeeded\r\nTelnet to device 172.24.101.12 via Proxy Server(10.10.1.43)\nTelnet to device 172.24.101.12 successfully via Proxy Server(10.10.1.43)\nReturn from Device:[ Username:]\nSending Username:nb\nReturn from Device:[Password:]\nSending Password:******\nReturn from Device:[BJ_Dis_SW2>]\nSending \"enable\" command\nReturn from Device:[Password:]\nSending Enable Password:******\nReturn from Device:[BJ_Dis_SW2#]\nSending \"enable\" command\nReturn from Device:[BJ_Dis_SW2#]\nSending \"terminal length 0\" command\nReturn from Device:[BJ_Dis_SW2#]\nSending \"show run\" command\nReceived:BJ_Dis_SW2#show run\r\nBuilding configuration...\r\n\r\r\n\r\nSending \"exit\" command\nTelnet to device 172.24.101.12 disconnected.\nUpdate configuration file of BJ_Dis_SW2 successfully,(0.546s)\r\nDiscovery of 172.24.101.12 complete"}]
 
 
# Step 10: Logout to NetBrain API server
print(netbrain_restful_lib.logoutSession(nb_url, token))

Removing Devices via APIs

This section covers the use case to remove devices from NetBrain domain via APIs.

Workflow

The pseudo-code below describes the main flow in the API script to remove devices.

1. Login to NetBrain API server and get session
2. Set the current domain
3. Delete device from domain
4. Confirm that device is no longer in workspace
5. Logout to NetBrain API server

API Script 

You can refer to the script below to call relative APIs. This sample script first imports a python library maintained by NetBrain and you need to write your own one in practical use.

import netbrain_restful_lib
import time
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
   
user = "Demo"      
pwd = "Netbrain1"      
nb_url = "http(s)://<IP address of NetBrain Web Server>/"           
headers = {'Content-Type''application/json''Accept''application/json'}
tenantName = "Initial Tenant"
domainName = "Use Case"
 
   
#################################################################
   
# Step 1: Login to NetBrain API server and get session
print("Step 1")
token = netbrain_restful_lib.loginSession(nb_url, user, pwd)
print("Token: "+token)
print(netbrain_restful_lib.getTenants(nb_url, token))
print(netbrain_restful_lib.getDomains(nb_url, token))
#Sample results
# token : adf5b560-8e9c-4114-87bb-2eed24f0faff
# tenantList : [{'tenantId': 'a50af282-fa02-012f-a7f7-e410cc7c29d3', 'tenantName': 'Initial Tenant'}]
# domainList : [{'domainName': 'UseCase', 'domainId': '4f747a8b-71da-404b-a05c-e8505aa14725'}]
    
tenantId = 'df20d549-d8ae-52ee-b842-036158d1083d'
domainId = '9df0d29e-7792-482d-a590-0620f4e8788f'
    
  
# Step 2: Set current domain
print("Step 2")
print(netbrain_restful_lib.loginDomain(nb_url, token, tenantId, domainId))
# Sample results
# loginDomain: Login successfully!
    
  
# Step 3. Delete device from domain
print("Step 3")
deviceName="NBUSMA-SW1"
print(netbrain_restful_lib.deleteDevice(nb_url, token, deviceName))
# Sample results
# Delete Device: Success
 
# Step 4. Confirm that device is no longer in workspace
print("Step 4")
deviceName="NBUSMA-SW1"
print(netbrain_restful_lib.getDevice(nb_url, token, deviceName))
# Sample result should show NULL device list
# Get Devices: []
 
# Step 5: Logout to NetBrain API server
print("Step 5")
print(netbrain_restful_lib.logoutSession(nb_url, token))