VMware Cloud Community
mark_l_zhang
Enthusiast
Enthusiast

ESXi 6.5 Kickstart Python Script joining to vCenter VCSA6.5

Please refer my documents on ESXi 5.5 and 6.0.

ESXi Kickstart Python Script joining to vCenter VCSA6.5

Now please check the ESXi 6.5 join the vCenter VCSA 6.5 through the python script.

The ESXi 6.5 has the python 3.0, so the previous scripts failed due to different language.

These are some tips I tried to update the scripts:

1 use the 2to3.

2 replace urllib2 urllib.request.

3 bytes in python3 instead of str in python2.

OK. Check the following codes:

import sys,re,os,urllib.request,urllib.parse,urllib.error,base64,syslog,socket, ssl

def stringToBase64(s):
return base64.b64encode (s.encode ('utf-8'))

def base64ToString(b):
return base64.b64decode (b).decode ('utf-8')

# vCenter server
vcenter_server = VCSA_IP

# vCenter Cluster path
cluster = Cluster

# vCenter credentials using encoded base64 password
vc_username = "Account"
vc_encodedpassword = "Password"
vc_password = base64ToString (vc_encodedpassword)

# ESX(i) credentials using encoded base64 password
host_username = "root"
host_encodedpasssword = "Hash"
host_password = base64ToString (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 = 'JOIN-VC'

syslog.syslog (syslogGhetto + ' Starting joinvCenter process - ' + url)

# Code to build opener with HTTP Basic Authentication
try:
passman = urllib.request.HTTPPasswordMgrWithDefaultRealm ()
passman.add_password (None, url, vc_username, vc_password)
authhandler = urllib.request.HTTPBasicAuthHandler (passman)
opener = urllib.request.build_opener (authhandler)
urllib.request.install_opener (opener)
except IOError as 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 = urllib.request.Request (url)
page = urllib.request.urlopen (req)
page_content = page.read ()
except IOError as e:
opener.close ()
syslog.syslog (syslogGhetto + ' Failed to retrieve MOB data')
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.parse.urlencode (params)
req = urllib.request.Request (url, e_params, headers={"Cookie": cookie})
page = urllib.request.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
# xml = '%hostname%sha%user%pass1'
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 as 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': ''}
e_params = urllib.parse.urlencode (params)
req = urllib.request.Request (url, e_params, headers={"Cookie": cookie})
page = urllib.request.urlopen (req).read ()
except IOError as 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.parse.urlencode (params)
req = urllib.request.Request (url, e_params, headers={"Cookie": cookie})
page = urllib.request.urlopen (req).read ()
sys.exit (0)
time.sleep (20)

0 Kudos
0 Replies