python: validate kerberos ticket
Question: I'm wondering - if anyone has an elegant solution to checking for a valid
Kerberos ticket using Python. I'm not seeing anyway with `kinit` or `klist`
that will show if a ticket is expired with a return code but I could run
`klist` and use a regex for the output .


Answer: You've got two options: the first is to use 'klist -s' and check the return
code. The nicer option is to use the [python-
krbV](https://pypi.python.org/pypi/python-krbV) module:

    
    
    import krbV
    
    def has_ticket():
        '''
        Checks to see if the user has a valid ticket.
        '''
        ctx = krbV.default_context()
        cc = ctx.default_ccache()
        try:
            princ = cc.principal()
            retval = True
        except krbV.Krb5Error:
            retval = False
    
        return retval
    

