def kinit(username=None, password=None, realm=None, exe=None, keytab=None,<EOL>krb5ccname=None, verbose=None):
<EOL>if exe is None:<EOL><INDENT>exe = which('<STR_LIT>')<EOL><DEDENT>if keytab is None:<EOL><INDENT>keytab = os.environ.get('<STR_LIT>', None)<EOL>if keytab is None or not os.path.isfile(keytab):<EOL><INDENT>keytab = None<EOL><DEDENT><DEDENT>if keytab:<EOL><INDENT>try:<EOL><INDENT>principals = parse_keytab(keytab)<EOL><DEDENT>except KerberosError:<EOL><INDENT>pass<EOL><DEDENT>else:<EOL><INDENT>if username is None and len(principals) == <NUM_LIT:1>:<EOL><INDENT>username = principals[<NUM_LIT:0>][<NUM_LIT:0>]<EOL><DEDENT>if username in list(zip(*principals))[<NUM_LIT:0>]:<EOL><INDENT>idx = list(zip(*principals))[<NUM_LIT:0>].index(username)<EOL>realm = principals[idx][<NUM_LIT:1>]<EOL><DEDENT>else:<EOL><INDENT>keytab = None<EOL><DEDENT><DEDENT><DEDENT>if not sys.stdout.isatty() and not _IPYTHON and (<EOL>username is None or<EOL>(not keytab and password is None)<EOL>):<EOL><INDENT>raise KerberosError("<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT>if realm is None:<EOL><INDENT>realm = '<STR_LIT>'<EOL><DEDENT>if username is None:<EOL><INDENT>verbose = True<EOL>username = input("<STR_LIT>"<EOL>"<STR_LIT>".format(realm))<EOL><DEDENT>identity = '<STR_LIT>'.format(username, realm)<EOL>if not keytab and password is None:<EOL><INDENT>verbose = True<EOL>password = getpass.getpass(prompt="<STR_LIT>".format(identity))<EOL><DEDENT>if keytab:<EOL><INDENT>cmd = [exe, '<STR_LIT>', '<STR_LIT>', keytab, identity]<EOL><DEDENT>else:<EOL><INDENT>cmd = [exe, identity]<EOL><DEDENT>if krb5ccname:<EOL><INDENT>krbenv = {'<STR_LIT>': krb5ccname}<EOL><DEDENT>else:<EOL><INDENT>krbenv = None<EOL><DEDENT>kget = subprocess.Popen(cmd, env=krbenv, stdout=subprocess.PIPE,<EOL>stdin=subprocess.PIPE)<EOL>if not keytab:<EOL><INDENT>kget.communicate(password.encode('<STR_LIT:utf-8>'))<EOL><DEDENT>kget.wait()<EOL>retcode = kget.poll()<EOL>if retcode:<EOL><INDENT>raise subprocess.CalledProcessError(kget.returncode, '<STR_LIT:U+0020>'.join(cmd))<EOL><DEDENT>if verbose:<EOL><INDENT>print("<STR_LIT>".format(identity))<EOL><DEDENT>
Initialise a kerberos (krb5) ticket.

    This allows authenticated connections to, amongst others, NDS2
    services.

    Parameters
    ----------
    username : `str`, optional
        name of user, will be prompted for if not given.

    password : `str`, optional
        cleartext password of user for given realm, will be prompted for
        if not given.

    realm : `str`, optional
        name of realm to authenticate against, read from keytab if available,
        defaults to ``'LIGO.ORG'``.

    exe : `str`, optional
        path to kinit executable.

    keytab : `str`, optional
        path to keytab file. If not given this will be read from the
        ``KRB5_KTNAME`` environment variable. See notes for more details.

    krb5ccname : `str`, optional
        path to Kerberos credentials cache.

    verbose : `bool`, optional
        print verbose output (if `True`), or not (`False)`; default is `True`
        if any user-prompting is needed, otherwise `False`.

    Notes
    -----
    If a keytab is given, or is read from the ``KRB5_KTNAME`` environment
    variable, this will be used to guess the username and realm, if it
    contains only a single credential.

    Examples
    --------
    Example 1: standard user input, with password prompt::

    >>> kinit('albert.einstein')
    Password for albert.einstein@LIGO.ORG:
    Kerberos ticket generated for albert.einstein@LIGO.ORG

    Example 2: extract username and realm from keytab, and use that
    in authentication::

    >>> kinit(keytab='~/.kerberos/ligo.org.keytab', verbose=True)
    Kerberos ticket generated for albert.einstein@LIGO.ORG