SHARE
TWEET

Untitled

a guest Aug 22nd, 2019 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import subprocess
  2. import getpass
  3. import os
  4. from app import mylogging as log
  5.  
  6. def exists(path):
  7.     """Test whether a path exists.  Returns False for broken symbolic links"""
  8.     try:
  9.         st = os.stat(path)
  10.     except os.error:
  11.         return False
  12.     return True
  13.  
  14. def check_and_reinit_kticket():
  15.     has_kticket = has_kerberos_ticket()
  16.     if debug_klist:
  17.         log.info("Has k-ticket or not: {}".format(has_kticket))
  18.     if has_kticket:
  19.         if debug_klist:
  20.             log.info("Renewing K-ticket")
  21.             reinit_kticket_func()
  22.         pass
  23.     else:
  24.         reinit_kticket_func()
  25.  
  26. def reinit_kticket_func():
  27.     """
  28.     USAGE.
  29.         $ ktutil
  30.         >> addent -password -p <user@server> -k 1 -e aes256-cts
  31.         >> [Enter Password]
  32.         >> wkt <userID>.keytab
  33.         >> exit
  34.  
  35.         $ kinit <userID> -k -t <userID>.keytab
  36.         $ klist
  37.         >>  Ticket cache: FILE:/tmp/krb5cc_928758424
  38.         >>  Default principal: <user@server>
  39.  
  40.     """
  41.     try:
  42.         # For service id's
  43.         username = getpass.getuser()
  44.         homedir = os.environ['HOME']
  45.         # For personal id's enable below line
  46.         user = username[:1]
  47.         if user != 's':
  48.             username = username.upper()
  49.         username_lo = username.lower()
  50.         keytab = "./" + username + ".keytab"
  51.         command = "kinit " + username + " -kt " + keytab
  52.         p1 = subprocess.Popen(command, shell=True) # USE THIS LINE FOR OWN UDER ID's
  53.         p1.wait()
  54.         log.info("Renewing Kticket (first try)")
  55.         log.info(" - homedir : {0}".format(keytab))
  56.         log.info(" - command : {0}".format(command))
  57.         if not has_kerberos_ticket():
  58.             keytab = "./" + username_lo + ".keytab"
  59.             command = "kinit " + username + " -kt " + keytab
  60.             p1 = subprocess.Popen(command, shell=True)  # USE THIS LINE FOR OWN UDER ID's
  61.             p1.wait()
  62.             log.info("Renewing Kticket (second try)")
  63.             log.info(" - homedir : {0}".format(keytab))
  64.             log.info(" - command : {0}".format(command))
  65.         if not has_kerberos_ticket():
  66.             log.info("<Err> : Kinit: Failed to reinit kticket.", level='error')
  67.         else:
  68.             log.info("Done. All set")
  69.  
  70.     except Exception as e:
  71.         log.info("<Err> Kinit: {0}".format(e), level='error')
  72.  
  73. def has_kerberos_ticket():
  74.     return True if subprocess.call(['klist', '-s']) == 0 else False
  75.  
  76. if __name__ == "__main__":
  77.     check_and_reinit_kticket()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top