-
Notifications
You must be signed in to change notification settings - Fork 0
/
n9k-spark.py
65 lines (57 loc) · 2.01 KB
/
n9k-spark.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
import requests
import json
import time
##Spark API call to send a message to the roomId.
def sparkCall(mymsg):
url = "https://api.ciscospark.com/v1/messages"
#payload = "{\n\t\"roomId\": \"Y2lzY29zcGFyazovL3VzL1JPT00vMjE0ODYwNTAtZjZkMC0xMWU1LThmOWYtYmQ5ZjQ4OTI3OGZh\",\n\t\"text\": \"Hi John\"\n}"
payload = {
"roomId": "Y2lzY29zcGFyazovL3VzL1JPT00vZWFjZDc2NjAtZmRlNS0xMWU1LWFjZGItYjlhMzIwZjE0NDAw",
"text": mymsg
}
headers = {
'content-type': "application/json",
'authorization': "Bearer ODA0MjA1ZTMtZDBhNi00NTYzLWE4NjUtZmY0YTIxZTY2NWI3MzQ4YTRkMDYtN2Nm",
'cache-control': "no-cache",
'postman-token': "1a5747a0-8dff-d146-3724-c30bdfe5080e"
}
response = requests.request("POST", url, data=json.dumps(payload), headers=headers)
## showCommand function. Takes a show command and returns the results
def showCommand(mycmd):
url='http://198.18.134.17/ins'
switchuser='admin'
switchpassword='Cisco321'
myheaders={'content-type':'application/json'}
payload={
"ins_api": {
"version": "1.0",
"type": "cli_show",
"chunk": "0",
"sid": "1",
"input": mycmd,
"output_format": "json"
}
}
response = requests.post(url,data=json.dumps(payload), headers=myheaders,auth=(switchuser,switchpassword)).json()
print (json.dumps(response,indent=4))
return response
## end def showCommand(mycmd): function
cmd = "show vlan"
while(1):
response= showCommand(cmd)
vlanData = response["ins_api"]["outputs"]["output"]["body"]["TABLE_vlanbrief"]["ROW_vlanbrief"]
#print (vlanData)
for vlanshowinfovlanid in vlanData:
vlanNum = int(vlanshowinfovlanid["vlanshowbr-vlanid-utf"])
vlanShutState = vlanshowinfovlanid["vlanshowbr-shutstate"]
print ()
print ("vlanNum: ",vlanNum)
print ("vlanShutState: ", vlanShutState)
if (vlanNum == 45):
if (vlanShutState == "shutdown"):
print("OH NO, VLAN 45 is SHUTDOWN")
sparkCall("OH NO, VLAN 45 is SHUTDOWN")
if (vlanShutState == "noshutdown"):
print("Yay, VLAN 45 is UP")
sparkCall("Yay, VLAN 45 is UP")
time.sleep(10)