1 year ago

#240096

test-img

Walner Borbon

Change VCenter administrator@vsphere.local password Python VMware vsphere

Some time ago I was working to rotate the password of the administrator@vsphere.local user in the Vcenter, but there isn't a lot of information about how to do that with Python. I did with LDAP protocol, I just want to create this to future references if someone need it.

import ldap
import os
import sys

vcenter = "the_IP_OR_Host_Name_of_the_VC"
ad_server = "ldap://"+vcenter
userAccount = 'Administrator'
ad_dn = "cn="+userAccount+",cn=users,dc=vsphere,dc=local"

username = 'CPMS_Test@vsphere.local'
old_pwd = 'The_Actual_PAss_of_Administrator'
new_pwd = 'New_Pass_of_the_Account'

# Lets initialize the LDAP, in our case is the same VC
l = ldap.initialize(ad_server)
#LDAP settings, Im not using SSL, but you can use it
l.protocol_version = ldap.VERSION3
l.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_DEMAND)
l.set_option(ldap.OPT_X_TLS_NEWCTX, 0)
l.simple_bind_s(ad_dn.format(username), old_pwd)

#lets run the update of the pass
password_value_old = old_pwd.encode("utf-8")
password_value = new_pwd.encode("utf-8")

mod_list = [
    (ldap.MOD_DELETE, 'userpassword', [password_value_old]),
    (ldap.MOD_ADD, 'userpassword', [password_value]),
]
result = l.modify_s(ad_dn.format(username), mod_list)
print(result.info)

python

ldap

vmware

vsphere

vcenter

0 Answers

Your Answer

Accepted video resources