-
Notifications
You must be signed in to change notification settings - Fork 0
/
cisco_devnet_dna.py
114 lines (86 loc) · 3.26 KB
/
cisco_devnet_dna.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import sys
import json
import requests
import os
from requests.auth import HTTPBasicAuth
requests.packages.urllib3.disable_warnings()
DNAC=os.environ.get('DNAC','sandboxdnac.cisco.com')
DNAC_PORT=os.environ.get('DNAC_PORT',443)
DNAC_USER=os.environ.get('DNAC_USER','devnetuser')
DNAC_PASSWORD=os.environ.get('DNAC_PASSWORD','Cisco123!')
def get_auth_token(controller_ip=DNAC, username=DNAC_USER, password=DNAC_PASSWORD):
""" Authenticates with controller and returns a token to be used in subsequent API invocations
"""
login_url = "https://{0}:{1}/dna/system/api/v1/auth/token".format(controller_ip, DNAC_PORT)
result = requests.post(url=login_url, auth=HTTPBasicAuth(DNAC_USER, DNAC_PASSWORD), verify=False)
result.raise_for_status()
token = result.json()["Token"]
return {
"controller_ip": controller_ip,
"token": token
}
def create_url(path, controller_ip=DNAC):
""" Helper function to create a DNAC API endpoint URL
"""
return "https://%s:%s/api/v1/%s" % (controller_ip, DNAC_PORT, path)
def get_create_network_device():
cliTransport = input("Enter cliTransport method = ")
enablePassword = input("Enter enablePassword: ")
ipAddress = input("Enter ip address: ")
password = input("Enter Password: ")
snmpAuthPassphrase = input("Enter SNMP Auth Passphrase: ")
snmpAuthProtocol = "v2"
snmpMode = "AuthPriv"
snmpPrivPassphrase = input("Enter SNMP Priv Passphrase: ")
snmpPrivProtocol = "v2"
snmpROCommunity = input("Enter SNMP RO Comm: ")
snmpRWCommunity = input("Enter SNMP Rw Comm: ")
get_body = {
"cliTransport":cliTransport,
"enablePassword":enablePassword,
"ipAddress":[ipAddress],
"password":password,
"snmpAuthPassphrase":snmpAuthPassphrase,
"snmpAuthProtocol":snmpAuthProtocol,
"snmpMode":snmpMode,
"snmpPrivPassphrase": snmpPrivPassphrase,
"snmpPrivProtocol": snmpPrivProtocol,
"snmpROCommunity": snmpROCommunity,
"snmpRWCommunity": snmpRWCommunity,
}
return json.dumps(get_body)
body = get_create_network_device
def get_url(url):
url = create_url(path=url)
print(url)
token = get_auth_token()
headers = {'X-auth-token' : token['token']}
try:
response = requests.get(url, headers=headers, verify=False)
except requests.exceptions.RequestException as cerror:
print("Error processing request", cerror)
sys.exit(1)
return response.json()
def post_url(url):
url = create_url(path=url)
print(url)
token = get_auth_token()
headers = {'X-auth-token' : token['token']}
try:
response = requests.post(url, headers=headers, verify=False, body=get_create_network_device)
except requests.exceptions.RequestException as cerror:
print("Error processing request", cerror)
sys.exit(1)
return response.json()
def list_network_devices():
return get_url("network-device")
#datatype = type(json_body)
#print(datatype)
#print(body)
def main ():
network_device = list_network_devices()
#print(network_device)
for device in network_device['response']:
print(device['id'], device['managementIpAddress'])
if __name__ == "__main__":
main()