When the hostname of a device in your live network changes, this device need to be removed and then re-discovered to NetBrain. You can utilize NetBrain APIs to execute the domain management tasks of this kind.
The pseudo-code below describes the main flow in the API script to execute the change.
1. Login to NetBrain API server and get session
2. Set the current domain
3. Remove device from workspace
4. Confirm the device is removed from workspace
5. Call GetAllDiscoveryTasks and pick up one task
6. Call AddIPforDiscovery to add its IP and/or credentials into the task
7. Call GetAllIPsForDiscovery and confirm that all IPs are added successfully
8. Call RunDiscoveryNow to run the discovery task
9. Keep calling GetDiscoveryTaskStatus each one minute till the task is completed or completed with error
10. Call GetDiscoveryTaskResult to get all IPs the task has tried to discover
11. 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
12. Logout to NetBrain API server
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.
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 = "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
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: Call GetAllDiscoveryTasks and pick up one task
print("Step 5")
print(netbrain_restful_lib.getDiscoveryTasks(nb_url, token))
# 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"}]
taskId = "e86e73da-1829-fd8e-4f83-98f524533779"
# Step 6: For each newly added device, call AddIPforDiscovery to add its IP and/or credentials into the task
print("Step 6")
data=[{"IP":"1.1.1.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 7: Call GetAllIPsForDiscovery and confirm that all IPs are added successfully
print("Step 7")
print(netbrain_restful_lib.getIPinDiscoveryTask(nb_url, token, taskId))
# Sample results
# getIPinDiscoveryTask: ["192.168.0.1"]
# Step 8: Call RunDiscoveryNow to run the discovery task
print("Step 8")
print(netbrain_restful_lib.runDiscoveryTask(nb_url, token, taskId))
time.sleep(2)
# Sample results
# runDiscoveryTask: Success
# Step 9: Keep calling GetDiscoveryTaskStatus each one minute till the task is completed or completed with error
print("Step 9")
status = netbrain_restful_lib.getDiscoveryRunningStatus(nb_url, token, taskId)
print("getDiscoveryRunningStatus: "+status)
while ("2" in netbrain_restful_lib.getDiscoveryRunningStatus(nb_url, token, taskId)):
print("Discovery Task still running")
time.sleep(15)
# 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 9: Call GetDiscoveryTaskResult to get all IPs the task has tried to discover
print("Step 9")
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 10: For each IP in results, check if results failed. If (Telnet/SSH status failed), call GetLiveAccessLog and print out the live access log
print("Step 10")
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 12: Logout to NetBrain API server
print("Step 12")
print(netbrain_restful_lib.logoutSession(nb_url, token))