forked from lamw/vmware-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjoinvCenter.py
More file actions
executable file
·152 lines (129 loc) · 5.73 KB
/
joinvCenter.py
File metadata and controls
executable file
·152 lines (129 loc) · 5.73 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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import sys,re,os,urllib,urllib2,base64,syslog,socket
# Author: William Lam
# Website: www.williamlam.com
# Product: VMware ESXi
# Description: Python script to call into vSphere MOB to add ESXi host to VC
# Reference: http://www.williamlam.com/2011/03/how-to-automatically-add-esxi-host-to.html
# vCenter server
vcenter_server = "vcenter51-1.primp-industries.com"
# vCenter Cluster path
cluster = "datacenter/host/cluster"
# vCenter credentials using encoded base64 password
vc_username = "vcjoin"
vc_encodedpassword = "TXlTdXBlckR1cGVyU2VjcmV0UGFzc3dvcmRZbw=="
vc_password = base64.b64decode(vc_encodedpassword)
# ESX(i) credentials using encoded base64 password
host_username = "root"
host_encodedpasssword = "dm13YXJl"
host_password = base64.b64decode(host_encodedpasssword)
### DO NOT EDIT PAST HERE ###
# vCenter mob URL for findByInventoryPath
url = "https://" + vcenter_server + "/mob/?moid=SearchIndex&method=findByInventoryPath"
# Create global variables
global passman,authhandler,opener,req,page,page_content,nonce,headers,cookie,params,e_params,syslogGhetto,clusterMoRef
# syslog key for eaiser troubleshooting
syslogGhetto = 'GHETTO-JOIN-VC'
syslog.syslog(syslogGhetto + ' Starting joinvCenter process - ' + url)
# Code to build opener with HTTP Basic Authentication
try:
passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None,url,vc_username,vc_password)
authhandler = urllib2.HTTPBasicAuthHandler(passman)
opener = urllib2.build_opener(authhandler)
urllib2.install_opener(opener)
except IOError, e:
opener.close()
syslog.syslog(syslogGhetto + ' Failed HTTP Basic Authentication!')
sys.exit(1)
else:
syslog.syslog(syslogGhetto + ' Succesfully built HTTP Basic Authentication')
# Code to capture required page data and cookie required for post back to meet CSRF requirements
# Thanks to user klich - http://communities.vmware.com/message/1722582#1722582
try:
req = urllib2.Request(url)
page = urllib2.urlopen(req)
page_content= page.read()
except IOError, e:
opener.close()
syslog.syslog(syslogGhetto + ' Failed to retrieve MOB data -> ' + str(e.args))
sys.exit(1)
else:
syslog.syslog(syslogGhetto + ' Succesfully requested MOB data')
# 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 search for vCenter Cluster
params = {'vmware-session-nonce':nonce,'inventoryPath':cluster}
e_params = urllib.urlencode(params)
req = urllib2.Request(url, e_params, headers={"Cookie":cookie})
page = urllib2.urlopen(req).read()
clusterMoRef = re.search('domain-c[0-9]*',page)
if clusterMoRef:
syslog.syslog(syslogGhetto + ' Succesfully located cluster "' + cluster + '"!')
else:
opener.close()
syslog.syslog(syslogGhetto + ' Failed to find cluster "' + cluster + '"!')
sys.exit(1)
# Code to compute SHA1 hash
cmd = "openssl x509 -sha1 -in /etc/vmware/ssl/rui.crt -noout -fingerprint"
tmp = os.popen(cmd)
tmp_sha1 = tmp.readline()
tmp.close()
s1 = re.split('=',tmp_sha1)
s2 = s1[1]
s3 = re.split('\n', s2)
sha1 = s3[0]
if sha1:
syslog.syslog(syslogGhetto + ' Succesfully computed SHA1 hash: "' + sha1 + '"!')
else:
opener.close()
syslog.syslog(syslogGhetto + ' Failed to compute SHA1 hash!')
sys.exit(1)
# Code to create ConnectHostSpec
xml = '<spec xsi:type="HostConnectSpec"><hostName>%hostname</hostName><sslThumbprint>%sha</sslThumbprint><userName>%user</userName><password>%pass</password><force>1</force></spec>'
# Code to extract IP Address to perform DNS lookup to add FQDN to vCenter
hostip = socket.gethostbyname(socket.gethostname())
if hostip:
syslog.syslog(syslogGhetto + ' Successfully extracted IP Address ' + hostip.strip())
else:
opener.close()
syslog.syslog(syslogGhetto + ' Failed to extract IP Address!')
sys.exit(1)
try:
host = socket.getnameinfo((hostip, 0), 0)[0]
except IOError, e:
syslog.syslog(syslogGhetto + ' Failed to perform DNS lookup for ' + hostipt.strip())
sys.exit(1)
else:
syslog.syslog(syslogGhetto + ' Successfully performed DNS lookup for ' + hostip.strip() + ' is ' + host)
xml = xml.replace("%hostname",host)
xml = xml.replace("%sha",sha1)
xml = xml.replace("%user",host_username)
xml = xml.replace("%pass",host_password)
# Code to join host to vCenter Cluster
try:
url = "https://" + vcenter_server + "/mob/?moid=" + clusterMoRef.group() + "&method=addHost"
params = {'vmware-session-nonce':nonce,'spec':xml,'asConnected':'1','resourcePool':'','license':''}
syslog.syslog(syslogGhetto + ' ' + url)
e_params = urllib.urlencode(params)
req = urllib2.Request(url, e_params, headers={"Cookie":cookie})
page = urllib2.urlopen(req).read()
except IOError, e:
opener.close()
syslog.syslog(syslogGhetto + ' Failed to join vCenter!')
syslog.syslog(syslogGhetto + ' HOSTNAME: ' + host)
syslog.syslog(syslogGhetto + ' USERNAME: ' + host_username)
#syslog.syslog(syslogGhetto + ' PASSWORD: ' + host_password)
sys.exit(1)
else:
syslog.syslog(syslogGhetto + ' Succesfully joined vCenter!')
syslog.syslog(syslogGhetto + ' Logging off vCenter')
url = "https://" + vcenter_server + "/mob/?moid=SessionManager&method=logout"
params = {'vmware-session-nonce':nonce}
e_params = urllib.urlencode(params)
req = urllib2.Request(url, e_params, headers={"Cookie":cookie})
page = urllib2.urlopen(req).read()
sys.exit(0)