Skip to content

Instantly share code, notes, and snippets.

@lamw
Created February 18, 2014 05:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lamw/9065097 to your computer and use it in GitHub Desktop.
Save lamw/9065097 to your computer and use it in GitHub Desktop.
vSphere SDK for Python to start NTP Service on an ESXi host
#!/usr/bin/python
# William Lam
# www.virtuallyghetto.com
"""
vSphere Python SDK program demonstrating starting up ESXi Service
"""
from optparse import OptionParser, make_option
from pyVim.connect import SmartConnect, Disconnect
from pyVmomi import vmodl
from pyVmomi import vim
from datetime import timedelta
import argparse
import atexit
import sys
import datetime
def GetArgs():
"""
Supports the command-line arguments listed below.
"""
parser = argparse.ArgumentParser(description='Process args for retrieving all the Virtual Machines')
parser.add_argument('-s', '--host', required=True, action='store', help='Remote host to connect to')
parser.add_argument('-o', '--port', type=int, default=443, action='store', help='Port to connect on')
parser.add_argument('-u', '--user', required=True, action='store', help='User name to use when connecting to host')
parser.add_argument('-p', '--password', required=True, action='store', help='Password to use when connecting to host')
parser.add_argument('-x', '--vihost', required=True, action='store', help='Name of ESXi host as seen in vCenter Server')
args = parser.parse_args()
return args
def main():
"""
Simple command-line program demonstrating configuring & enabling NTP Service
"""
args = GetArgs()
try:
si = None
try:
si = SmartConnect(host=args.host,
user=args.user,
pwd=args.password,
port=int(args.port))
except IOError, e:
pass
if not si:
print "Could not connect to the specified host using specified username and password"
return -1
atexit.register(Disconnect, si)
content = si.RetrieveContent()
searchIndex = content.searchIndex
# quick/dirty way to find an ESXi host
host = searchIndex.FindByDnsName(dnsName=args.vihost, vmSearch=False)
# NTP manager on ESXi host
dateTimeManager = host.configManager.dateTimeSystem
# configure NTP Servers if not configured
#ntpServers = ['192.168.1.100','192.168.1.200']
#ntpConfig = vim.HostNtpConfig(server=ntpServers)
#dateConfig = vim.HostDateTimeConfig(ntpConfig=ntpConfig)
#dateTimeManager.UpdateDateTimeConfig(config=dateConfig)
# start ntpd service
serviceManager = host.configManager.serviceSystem
if dateTimeManager.dateTimeInfo.ntpConfig.server != []:
print "Starting ntpd service on " + args.vihost
serviceManager.StartService(id='ntpd')
else:
print "Error: Unable to start NTP Service because no NTP servers have not been configured"
except vmodl.MethodFault, e:
print "Caught vmodl fault : " + e.msg
return -1
except Exception, e:
print "Caught exception : " + str(e)
return -1
return 0
# Start program
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment