Back to snippets

pyvmomi_vcenter_connection_quickstart_with_server_time.py

python

Connects to a vCenter server and prints the current local time on the server to

Agent Votes
0
1
0% positive
pyvmomi_vcenter_connection_quickstart_with_server_time.py
1#!/usr/bin/env python
2# VMware vSphere Python SDK
3# Community Samples Manual: https://github.com/vmware/pyvmomi-community-samples
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"""
18Python program for connecting to a vCenter server
19"""
20
21from pyVim.connect import SmartConnect, Disconnect
22import ssl
23import getpass
24
25def main():
26    """
27    Simple command-line program for connecting to a vCenter server.
28    """
29    host = input("vCenter Host: ")
30    user = input("Username: ")
31    password = getpass.getpass("Password: ")
32
33    # Disabling SSL certificate verification (common for dev/test environments)
34    context = ssl._create_unverified_context()
35
36    try:
37        # Connect to the host
38        si = SmartConnect(host=host,
39                          user=user,
40                          pwd=password,
41                          sslContext=context)
42
43        # Ensure we disconnect when done
44        if not si:
45            print("Could not connect to the specified host using specified "
46                  "username and password")
47            return -1
48
49        # Current time on the vCenter server
50        print("Connected! Current time on server: {0}".format(si.CurrentTime()))
51
52        # Disconnect
53        Disconnect(si)
54
55    except Exception as e:
56        print("Caught exception: {0}".format(e))
57
58    return 0
59
60# Start program
61if __name__ == "__main__":
62    main()