-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgetAPICdn.py
63 lines (51 loc) · 1.84 KB
/
getAPICdn.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
#!/usr/bin/env Python
#
# cpaggen
#
import requests
import sys
import json
from pprint import pprint
http_header={"User-Agent" : "Chrome/17.0.963.46",
"Accept" : "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,text/png,*/*;q=0.5",
"Accept-Language" : "en-us,en;q=0.5",
"Accept-Charset" : "ISO-8859-1",
"Content-type": "application/x-www-form-urlencoded"
}
def getAPICCookie(ip_addr, username, password):
url = 'http://'+ip_addr+'/api/aaaLogin.xml'
http_header["Host"]=ip_addr
xml_string = "<aaaUser name='%s' pwd='%s'/>" % (username, password)
req = requests.post(url, data=xml_string, headers=http_header, verify=False)
rawcookie=req.cookies['APIC-cookie']
return rawcookie
def sendAPICRequest(ip_addr, cookie, apicurl):
url = 'http://'+ip_addr+apicurl
http_header["Host"]=ip_addr
cookies = {}
cookies['APIC-cookie'] = cookie
req = requests.get(url,headers=http_header,cookies=cookies)
return req.text
#################
# MAIN MODULE #
#################
if len(sys.argv) != 5:
ip=raw_input("IP address of APIC? ")
user=raw_input("Admin username? ")
password=raw_input("Password? ")
apicurl=raw_input('URL to GET? (e.g. /api/node/class/foo.json?query-target-filter=and(eq(foo.name,"my_name")) ')
else:
ip,user,password,apicurl = sys.argv[1:]
cookie=getAPICCookie(ip, user, password)
if cookie:
print "We have a cookie:\n %s\n" % cookie
r=sendAPICRequest(ip, cookie, apicurl)
#
# CRC is {u'imdata': [{u'rmonEtherStats': {u'attributes': {u'broadcastPkts': u'9',
# u'cRCAlignErrors'
#
if r:
parsed_json = json.loads(r)
pprint(parsed_json)
else:
print "That didn't work, we received no response back!"