forked from lamw/vmware-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddVirtualFlashResource.py
More file actions
executable file
·63 lines (52 loc) · 2.08 KB
/
addVirtualFlashResource.py
File metadata and controls
executable file
·63 lines (52 loc) · 2.08 KB
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
# Author: William Lam
# Website: www.williamlam.com
# Product: VMware vSphere + vSphere Flash Read Cache (vFRC)
# Description: Python script that calls vSphere MOB to add Virtual Flash Resource
# Reference: http://www.williamlam.com/2013/11/how-to-automate-configuration-of-vfrc.html
import sys,re,os,urllib,urllib2,base64
if len(sys.argv) == 1:
print "\n\tUsage: " + str(sys.argv[0]) + " [VFFS-UUID]\n"
sys.exit(1)
else:
vffsUuid = str(sys.argv[1])
# mob url
url = "https://localhost/mob/?moid=ha-vflash-manager&method=configureVFlashResource"
# mob login credentials
username = "root"
password = "vmware123"
# Create global variables
global passman,authhandler,opener,req,page,page_content,nonce,headers,cookie,params,e_params
# Code to build opener with HTTP Basic Authentication
passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None,url,username,password)
authhandler = urllib2.HTTPBasicAuthHandler(passman)
opener = urllib2.build_opener(authhandler)
urllib2.install_opener(opener)
### Code to capture required page data and cookie required for post back to meet CSRF requirements ###
try:
req = urllib2.Request(url)
page = urllib2.urlopen(req)
page_content= page.read()
except IOError, e:
opener.close()
sys.exit(1)
else:
print "Successfully connected to vSphere MOB"
# regex to get the vmware-session-nonce value from the hidden form entry
reg = re.compile('name="vmware-session-nonce" type="hidden" value="?([^\s^"]+)"')
nonce = reg.search(page_content).group(1)
# get the page headers to capture the cookie
headers = page.info()
cookie = headers.get("Set-Cookie")
# Code to create HostVFlashManagerVFlashResourceConfigSpec
xml = '<spec xsi:type="HostVFlashManagerVFlashResourceConfigSpec"><vffsUuid>' + vffsUuid + '</vffsUuid></spec>'
try :
params = {'vmware-session-nonce':nonce,'spec':xml}
e_params = urllib.urlencode(params)
req = urllib2.Request(url, e_params, headers={"Cookie":cookie})
page = urllib2.urlopen(req).read()
except IOError, e:
opener.close()
sys.exit(1)
else:
print "Successfully issued configureVFlashResource() with VFFS UUID " + vffsUuid + "\n"