Skip to content

Instantly share code, notes, and snippets.

@lrivallain
Last active Mar 4, 2017
Embed
What would you like to do?
This script retrieves informations from guestInfo.ovfEnv and print the OVF properties. In second time, it use OVF properties to reset the root password.
#!/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