Ejemplo n.º 1
0
def check_login(user, pw, host):
    try:
        client = VConnector(user=user, pwd=pw, host=host)
        client.connect()
        client.disconnect()
        del client
        return True
    except:
        client.disconnect()
        del client
        return False
Ejemplo n.º 2
0
#-------------------------------------------------------------------------------
# Name:        module1
# Purpose:
#
# Author:      Shoblin
#
# Created:     27.03.2019
# Copyright:   (c) Shoblin 2019
# Licence:     <your licence>
#-------------------------------------------------------------------------------


from __future__ import print_function
from vconnector.core import VConnector


username = '******'
password = '******'
hostname = 'msk-vcenter1.office.finam.ru'

client = VConnector(  user=username, pwd=password, host=hostname)
client.connect()
vms = client.get_vm_view()
print(vms.view)
client.disconnect()
Ejemplo n.º 3
0
class MainApp(object):
    """
    Main App class

    """
    def __init__(self):
        self.dialog = Dialog(autowidgetsize=True)
        self.dialog.add_persistent_args(['--no-mouse'])
        self.dialog.set_background_title(
            'Python vSphere Client version {}'.format(__version__))
        self.agent = None

    def about(self):
        welcome = ('Welcome to the Python vSphere Client version {}.\n\n'
                   'PVC is hosted on Github. Please contribute by reporting '
                   'issues, suggesting features and sending patches using '
                   'pull requests.\n\n'
                   'https://github.com/dnaeon/pvc')

        self.dialog.msgbox(title='Welcome', text=welcome.format(__version__))

    def login(self):
        """
        Login to the VMware vSphere host

        Returns:
            True on successful connect, False otherwise

        """
        form_text = ('Enter IP address or DNS name '
                     'of the VMware vSphere host you wish '
                     'to connect to.\n')

        elements = [
            pvc.widget.form.FormElement(label='Hostname'),
            pvc.widget.form.FormElement(label='Username'),
            pvc.widget.form.FormElement(label='Password', attributes=0x1),
        ]

        form = pvc.widget.form.Form(
            dialog=self.dialog,
            form_elements=elements,
            mixed_form=True,
            title='Login Details',
            text=form_text,
        )

        while True:
            code, fields = form.display()
            if code in (self.dialog.CANCEL, self.dialog.ESC):
                return False

            if not all(fields.values()):
                self.dialog.msgbox(
                    title='Error',
                    text='Invalid login details, please try again.\n')
                continue

            self.dialog.infobox(
                title='Establishing Connection',
                text='Connecting to {} ...'.format(fields['Hostname']),
            )

            self.agent = VConnector(
                host=fields['Hostname'],
                user=fields['Username'],
                pwd=fields['Password'],
            )

            try:
                self.agent.connect()
                text = '{} - {} - Python vSphere Client version {}'
                background_title = text.format(
                    self.agent.host, self.agent.si.content.about.fullName,
                    __version__)
                self.dialog.set_background_title(background_title)
                return True
            except Exception as e:
                if isinstance(e, pyVmomi.vim.MethodFault):
                    msg = e.msg
                else:
                    msg = e

                self.dialog.msgbox(title='Login failed',
                                   text='Failed to login to {}\n\n{}\n'.format(
                                       self.agent.host, msg))

    def disconnect(self):
        """
        Disconnect from the remote vSphere host

        """
        if not self.agent:
            return

        self.dialog.infobox(title='Disconnecting Connection',
                            text='Disconnecting from {} ...'.format(
                                self.agent.host))
        self.agent.disconnect()

    def run(self):
        try:
            self.about()
            if not self.login():
                return

            home = pvc.widget.home.HomeWidget(agent=self.agent,
                                              dialog=self.dialog)
            home.display()
        except KeyboardInterrupt:
            pass
        finally:
            self.disconnect()
Ejemplo n.º 4
0
Archivo: core.py Proyecto: hartsock/pvc
class MainApp(object):
    """
    Main App class

    """
    def __init__(self):
        self.dialog = Dialog(autowidgetsize=True)
        self.dialog.set_background_title(
            'Python vSphere Client version {}'.format(__version__)
        )
        self.agent = None

    def about(self):
        welcome = (
            'Welcome to the Python vSphere Client version {}.\n\n'
            'PVC is hosted on Github. Please contribute by reporting '
            'issues, suggesting features and sending patches using '
            'pull requests.\n\n'
            'https://github.com/dnaeon/pvc'
        )

        self.dialog.msgbox(
            title='Welcome',
            text=welcome.format(__version__)
        )

    def login(self):
        """
        Login to the VMware vSphere host

        Returns:
            True on successful connect, False otherwise

        """
        form_text = (
            'Enter IP address or DNS name '
            'of the VMware vSphere host you wish '
            'to connect to.\n'
        )

        elements = [
            pvc.widget.form.FormElement(label='Hostname'),
            pvc.widget.form.FormElement(label='Username'),
            pvc.widget.form.FormElement(label='Password', attributes=0x1),
        ]

        form = pvc.widget.form.Form(
            dialog=self.dialog,
            form_elements=elements,
            mixed_form=True,
            title='Login Details',
            text=form_text,
        )

        while True:
            code, fields = form.display()
            if code in (self.dialog.CANCEL, self.dialog.ESC):
                return False

            if not all(fields.values()):
                self.dialog.msgbox(
                    title='Error',
                    text='Invalid login details, please try again.\n'
                )
                continue

            self.dialog.infobox(
                title='Establishing Connection',
                text='Connecting to {} ...'.format(fields['Hostname']),
            )

            self.agent = VConnector(
                host=fields['Hostname'],
                user=fields['Username'],
                pwd=fields['Password'],
            )

            try:
                self.agent.connect()
                text = '{} - {} - Python vSphere Client version {}'
                background_title = text.format(
                    self.agent.host,
                    self.agent.si.content.about.fullName,
                    __version__
                )
                self.dialog.set_background_title(background_title)
                return True
            except Exception as e:
                self.dialog.msgbox(
                    title='Login failed',
                    text='Failed to login to {}\n\n{}\n'.format(self.agent.host, e.msg)
                )

    def run(self):
        self.about()
        if not self.login():
            return

        home = pvc.widget.home.HomeWidget(
            agent=self.agent,
            dialog=self.dialog
        )
        home.display()

        self.dialog.infobox(
            title='Disconnecting Connection',
            text='Disconnecting from {} ...'.format(self.agent.host)
        )
        self.agent.disconnect()