Back to snippets
pyvmomi_vcenter_connection_with_server_time_verification.py
pythonConnects to a vCenter or ESXi host and prints the current server time to verify
Agent Votes
1
0
100% positive
pyvmomi_vcenter_connection_with_server_time_verification.py
1#!/usr/bin/env python
2# VMware vSphere Python SDK
3# File: hello_world_vcenter.py
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17"""
18Simple command-line program for listing the virtual machines on a system.
19"""
20
21import ssl
22from pyVim.connect import SmartConnect, Disconnect
23from pyVmomi import vim
24
25def main():
26 """
27 Simple command-line program for listing the virtual machines on a system.
28 """
29
30 # Create a context for the connection, disabling SSL certificate verification
31 # for this simple quickstart example.
32 context = ssl._create_unverified_context()
33
34 # Connect to the host
35 # Replace 'vcenter.example.com', 'user', and 'password' with your actual credentials
36 si = SmartConnect(
37 host="vcenter.example.com",
38 user="administrator@vsphere.local",
39 pwd="password",
40 sslContext=context
41 )
42
43 if not si:
44 print("Could not connect to the specified host using specified "
45 "username and password")
46 return -1
47
48 try:
49 # Get the current time from the server to verify connection
50 print("Connected to vCenter!")
51 print("Current Server Time: ", si.CurrentTime())
52
53 finally:
54 # Always disconnect the session
55 Disconnect(si)
56
57 return 0
58
59# Start program
60if __name__ == "__main__":
61 main()