Advertisement
Guest User

sandbox_script

a guest
Nov 20th, 2017
748
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 KB | None | 0 0
  1. import requests
  2. import os
  3. import sys
  4. from optparse import OptionParser
  5. import subprocess
  6.  
  7.  
  8. def match_users(marathon_url, auth_user, auth_pass, app_type):
  9. print('\nUsing URL {} cross check app - {} for active users\n'.format(marathon_url, app_type))
  10. apps = requests.get(marathon_url, auth=(auth_user, auth_pass)).json()["apps"]
  11. for app in apps:
  12. labels = app.get('labels')
  13. id = app.get('id')
  14. if id and app_type in id:
  15. if labels and labels.get('user'):
  16. user = labels['user']
  17. env_user = subprocess.check_output(["sudo getent passwd {0} | cut -d ':' -f 5".format(user)],
  18. shell=True)
  19. if env_user:
  20. print("{} - {}".format(user, env_user))
  21. else:
  22. print("{0} - Not Found\n".format(user))
  23.  
  24.  
  25. if __name__ == '__main__':
  26. usage = "usage: python sandbox_users_check.py --marathon_url=marathon_url_for_apps --auth_user=man_username --auth_pass=password --app_type=sandbox"
  27. parser = OptionParser(usage=usage)
  28.  
  29. parser.add_option("--marathon_url",
  30. dest="url",
  31. action="store",
  32. help="path the the marathon (sandbox) applications")
  33.  
  34. parser.add_option("-u", "--auth_user",
  35. dest="auth_user",
  36. action="store",
  37. help="username to authenticate with")
  38.  
  39. parser.add_option("-p", "--auth_pass",
  40. dest="auth_pass",
  41. action="store",
  42. help="password to authenticate with")
  43.  
  44. parser.add_option("--app_type",
  45. dest="app_type",
  46. action="store",
  47. help="type of app to check for; example and default: sandbox")
  48. (options, args) = parser.parse_args()
  49.  
  50. marathon_url = options.url
  51. auth_user = options.auth_user
  52. auth_pass = options.auth_pass
  53. app_type = options.app_type
  54.  
  55. if not marathon_url:
  56. print "MARATHON url is mandatory"
  57. sys.exit(1)
  58. elif not auth_user:
  59. print "authentication username is mandatory"
  60. sys.exit(1)
  61. elif not auth_pass:
  62. print "authentication password is mandatory"
  63. sys.exit(1)
  64. elif not app_type:
  65. app_type = 'sandbox' # default app to look for
  66.  
  67. match_users(marathon_url, auth_user, auth_pass, app_type)
  68.  
  69. sys.exit(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement