Last active
November 3, 2023 06:35
-
-
Save lrivallain/77d9dda42bf77ddce1fc3bf2ee69e37a to your computer and use it in GitHub Desktop.
This script retrieves informations from guestInfo.ovfEnv and print the OVF properties. In second time, it use OVF properties to reset the root password.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
""" | |
This script retrieves information from guestInfo.ovfEnv and | |
print the OVF properties. | |
In second time, it use OVF properties to reset the root password. | |
""" | |
import subprocess | |
from xml.dom.minidom import parseString | |
from pprint import pprint | |
ovfenv_cmd="/usr/bin/vmtoolsd --cmd 'info-get guestinfo.ovfEnv'" | |
chpasswd_cmd = "/usr/sbin/chpasswd" | |
def get_ovf_properties(): | |
""" | |
Return a dict of OVF properties in the ovfenv | |
""" | |
properties = {} | |
xml_parts = subprocess.Popen(ovfenv_cmd, shell=True, | |
stdout=subprocess.PIPE).stdout.read() | |
raw_data = parseString(xml_parts) | |
for property in raw_data.getElementsByTagName('Property'): | |
key, value = [ property.attributes['oe:key'].value, | |
property.attributes['oe:value'].value ] | |
properties[key] = value | |
return properties | |
def change_linux_password(user, new_password): | |
""" | |
Change linux password for a user | |
""" | |
print("Setting new password for %s" % user) | |
_sp = subprocess.Popen([chpasswd_cmd], | |
stderr=subprocess.PIPE, stdin=subprocess.PIPE) | |
_sp.communicate(input='%s:%s' % (user, new_password)) | |
_sp.wait() | |
if _sp.returncode != 0: | |
print("Failed to set new password for %s" % user) | |
properties = get_ovf_properties() | |
pprint(properties, indent=1, width=80) | |
change_linux_password('root', properties['root_password']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment