def connect(self):
if self.connected:<EOL><INDENT>return<EOL><DEDENT>logger.debug(self.session)<EOL>if not self.session.is_alive():<EOL><INDENT>self._session = transport.SSHSession(self._device_handler)<EOL><DEDENT>defaults = {<EOL>'<STR_LIT:host>': None,<EOL>'<STR_LIT:port>': <NUM_LIT>,<EOL>'<STR_LIT>': <NUM_LIT:30>,<EOL>'<STR_LIT:username>': None,<EOL>'<STR_LIT:password>': None,<EOL>'<STR_LIT>': None,<EOL>'<STR_LIT>': False,<EOL>'<STR_LIT>': False,<EOL>'<STR_LIT>': False,<EOL>'<STR_LIT>': None,<EOL>}<EOL>defaults.update(self.connection_info)<EOL>disregards = ['<STR_LIT:class>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>']<EOL>defaults = {k: v for k, v in defaults.items() if k not in disregards}<EOL>if '<STR_LIT>' in defaults:<EOL><INDENT>defaults['<STR_LIT:host>'] = str(defaults.pop('<STR_LIT>'))<EOL><DEDENT>if '<STR_LIT:user>' in defaults:<EOL><INDENT>defaults['<STR_LIT:username>'] = str(defaults.pop('<STR_LIT:user>'))<EOL><DEDENT>defaults = {k: getattr(self, k, v) for k, v in defaults.items()}<EOL>try:<EOL><INDENT>self.session.connect(**defaults)<EOL><DEDENT>except Exception:<EOL><INDENT>if self.session.transport:<EOL><INDENT>self.session.close()<EOL><DEDENT>raise<EOL><DEDENT>@atexit.register<EOL>def cleanup():<EOL><INDENT>if self.session.transport:<EOL><INDENT>self.session.close()<EOL><DEDENT><DEDENT>
connect

        High-level api: opens the NetConf connection and exchanges
        capabilities. Since topology YAML file is parsed by BaseConnection,
        the following parameters can be specified in your YAML file.

        Parameters
        ----------

        host : `string`
            Hostname or IP address to connect to.
        port : `int`, optional
            By default port is 830, but some devices use the default SSH port
            of 22 so this may need to be specified.
        timeout : `int`, optional
            An optional keyed argument to set timeout value in seconds. By
            default this value is 30 seconds.
        username : `string`
            The username to use for SSH authentication.
        password : `string`
            The password used if using password authentication, or the
            passphrase to use for unlocking keys that require it.
        key_filename : `string`
            a filename where a the private key to be used can be found.
        allow_agent : `boolean`
            Enables querying SSH agent (if found) for keys. The default value
            is True.
        hostkey_verify : `boolean`
            Enables hostkey verification from ~/.ssh/known_hosts. The default
            value is False.
        look_for_keys : `boolean`
            Enables looking in the usual locations for ssh keys
            (e.g. ~/.ssh/id_*). The default value is True.
        ssh_config : `string`
            Enables parsing of an OpenSSH configuration file, if set to its
            path, e.g. ~/.ssh/config or to True. If the value is True,
            ncclient uses ~/.ssh/config. The default value is None.

        Raises
        ------

        Exception
            If the YAML file does not have correct connections section, or
            establishing transport to ip:port is failed, ssh authentication is
            failed, or other transport failures.

        Note
        ----

        There is no return from this method. If something goes wrong, an
        exception will be raised.


        YAML Example::

            devices:
                asr22:
                    type: 'ASR'
                    tacacs:
                        login_prompt: "login:"
                        password_prompt: "Password:"
                        username: "admin"
                    passwords:
                        tacacs: admin
                        enable: admin
                        line: admin
                    connections:
                        a:
                            protocol: telnet
                            ip: "1.2.3.4"
                            port: 2004
                        vty:
                            protocol : telnet
                            ip : "2.3.4.5"
                        netconf:
                            class: yang.connector.Netconf
                            ip : "2.3.4.5"
                            port: 830
                            username: admin
                            password: admin

        Code Example::

            >>> from pyats.topology import loader
            >>> testbed = loader.load('/users/xxx/xxx/asr22.yaml')
            >>> device = testbed.devices['asr22']
            >>> device.connect(alias='nc', via='netconf')
            >>>

        Expected Results::

            >>> device.nc.connected
            True
            >>> for iter in device.nc.server_capabilities:
            ...     print(iter)
            ...
            urn:ietf:params:xml:ns:yang:smiv2:RFC-1215?module=RFC-1215
            urn:ietf:params:xml:ns:yang:smiv2:SNMPv2-TC?module=SNMPv2-TC
            ...
            >>>