-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgenericREST.py
91 lines (72 loc) · 2.49 KB
/
genericREST.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
#!/usr/bin/env Python
#
# cpaggen
import urllib2
import base64
import sys
handlers = []
hh = urllib2.HTTPHandler()
hh.set_http_debuglevel(0)
handlers.append(hh)
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 createAuthHeader(username,password):
base64string = base64.encodestring('%s:%s' % (username, password))[:-1]
return ('Basic %s' % base64string)
def getAPICCookie(ip_addr, authheader, username, password):
url = 'http://'+ip_addr+'/api/aaaLogin.xml'
# create 'opener' (OpenerDirector instance)
opener = urllib2.build_opener(*handlers)
# Install the opener.
# Now all calls to urllib2.urlopen use our opener.
urllib2.install_opener(opener)
http_header["Host"]=ip_addr
xml_string = "<aaaUser name='%s' pwd='%s'/>" % (username, password)
req = urllib2.Request(url=url, data=xml_string, headers=http_header)
try:
response = urllib2.urlopen(req)
except urllib2.URLError, e:
print 'Failed to obtain auth cookie: %s' % (e)
return 0
else:
rawcookie=response.info().getheaders('Set-Cookie')
return rawcookie[0]
def sendAPICRequest(ip_addr, cookie, url):
url = 'https://'+ip_addr+url
opener = urllib2.build_opener(*handlers)
urllib2.install_opener(opener)
http_header["Host"]=ip_addr
http_header["Cookie"]=cookie
req = urllib2.Request(url=url,headers=http_header)
try:
response = urllib2.urlopen(req)
except urllib2.URLError, e:
print "URLLIB2 error:\n %s\n URL: %s\n Reason: %s" % (e, e.url, e.reason)
else:
return response
#################
# MAIN MODULE #
#################
if len(sys.argv) != 5:
ip = raw_input("IP: ")
user = raw_input("username: ")
password = raw_input("password: ")
url = raw_input("URL: ")
else:
ip, user, password, url = sys.argv[1:]
basicauth=createAuthHeader(user, password)
cookie=getAPICCookie(ip, basicauth, user, password)
if cookie:
print "We have a cookie:\n %s\n" % cookie
r=sendAPICRequest(ip, cookie, url)
results = r.read()
print results
fp = open("genericREST-script_results.xml", "w")
fp.write(results)
fp.close()
print
"\t ... done! Output written to genericREST-script_results.xml"