Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2017
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.19 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. ###################################################
  4. # File name: update_passwords.py #
  5. # Author: Martin Uribe #
  6. # Date created: 03/17/2017 #
  7. # Date last modified: 03/23/2017 #
  8. # Python Version: 2.6.5 #
  9. ###################################################
  10. """Script for updating the passwords of RIDS user accounts.
  11.  
  12. You have the option of updating the passwords for all of your RIDS accounts.
  13. This incules both the UNIX and RIDS passwords. The changes are pushed out
  14. for each environment, which takes time so make sure that you actually want
  15. to update them all.
  16.  
  17. Optionally you have the choice of updating a single account. You must
  18. specify the user that you want to update.
  19. """
  20. import getopt
  21. import os
  22. import sys
  23.  
  24. __version__ = '1.0.1'
  25.  
  26. __author__ = 'Martin Uribe'
  27. __email__ = 'martin.uribe AT mywork.com'
  28.  
  29.  
  30. def change_all(accounts):
  31. """Used for updating all of your RIDS accounts at once.
  32.  
  33. This function calls update_pass() while looping through all of your RIDS
  34. accounts.
  35.  
  36. :param accounts: List of all of your accounts on the system
  37. :return: None
  38. """
  39. for account in accounts:
  40. env = get_env(account, accounts)
  41. update_pass(account, env)
  42.  
  43.  
  44. def update_pass(user, env):
  45. """Used to make the actual changes to your accounts.
  46.  
  47. It will run the UNIX passwd command against the user account. It will
  48. then change its working directory to that of the environment to which
  49. the account belongs to and push out the updated password with the
  50. user_unix2rids perl script.
  51.  
  52. :param user: String containing the user account to modify
  53. :param env: String containing the environment that the account belongs to
  54. :return: None
  55. """
  56. push_update = 'sr -u apache ./user_unix2rids -update %s' % user
  57. change_passwd = 'sr passwd %s' % user
  58. env_path = '/usr/' + env + '/bin'
  59.  
  60. if os.path.isdir(env_path):
  61. os.chdir(env_path)
  62. print '[%s] %s:' % (env.upper(), user)
  63. # actually modify the system
  64. os.system(change_passwd)
  65. os.system(push_update)
  66. else:
  67. print '%s does not exist!' % env.upper()
  68.  
  69.  
  70. def get_accounts(user):
  71. """Used to get all of your the RIDS accounts.
  72.  
  73. These accounts are based on the account from which you are currently
  74. logged in with or the account that you specified at the command line
  75. with the --user flag.
  76.  
  77. :param user: String containing the user account to modify
  78. :return: Dictionary of user accounts associated with the user provided
  79. """
  80. with open('/etc/passwd', 'r') as p:
  81. # just had to play around with dictionary comprehensions :D
  82. accounts = dict((line.split(':')[0], line.split(':')[-1].split('/')[2].strip()) for line in p if user in line)
  83. return accounts
  84.  
  85.  
  86. def get_env(user, accounts):
  87. """Used to get the environment of the account.
  88.  
  89. I've made the assumption here that any account that doesn't have
  90. startExt as its shell is an env1 account for the Field Engineer,
  91. who is the one most likely to be using this script.
  92.  
  93. :param user: String containing the user account to look up
  94. :param accounts: Dictionary of accounts used to look-up the env for the account
  95. :return: String of the environment that the account belongs to
  96. """
  97. env = accounts[user]
  98. if 'env' not in env:
  99. env = 'env1'
  100. return env
  101.  
  102.  
  103. def info(user):
  104. """Used to display information on how to run this program
  105.  
  106. Displays a more detailed explanation of how to run the script and
  107. details all of the flags that it can accept.
  108.  
  109. :return: None
  110. """
  111. print '\nThis script will change the passwords for the given user account(s)'
  112. print 'on the system. If an optional argument is used, both the user and the'
  113. print 'env options must be provided.\n'
  114. print ' -h, --help: This help info'
  115. print ' -a, --all : Make changes to all of your accounts'
  116. print ' -u, --user: The username of the account that you want to modify'
  117. print '\n examples: ' + sys.argv[0] + ' -u ' + user
  118. print ' ' + sys.argv[0] + ' --user ' + user
  119.  
  120.  
  121. def main(argv):
  122. """Main entry point to the program
  123.  
  124. This function puts it all together.
  125.  
  126. :param argv: List of arguments that were past in when the script was ran
  127. :return: None
  128. """
  129. import getpass
  130.  
  131. usage = 'USAGE: ' + sys.argv[0] + ' --help --all [--user <user>]'
  132. user = getpass.getuser()
  133. accounts = get_accounts(user)
  134. flags = len(argv)
  135. run_all = False
  136.  
  137. if 0 <= flags <= 2:
  138. try:
  139. opts, args = getopt.getopt(argv, 'hau:', ['help', 'all', 'user='])
  140. except getopt.GetoptError:
  141. # some funky stuff was put in, get out now!
  142. print 'Invalid argument used!'
  143. print usage
  144. sys.exit(2)
  145.  
  146. # cycle through the options
  147. for opt, arg in opts:
  148. if opt in ('-h', '--help'):
  149. print usage
  150. info(user)
  151. sys.exit()
  152. elif opt in ('-a', '--all'):
  153. run_all = True
  154. elif opt in ('-u', '--user'):
  155. user = arg
  156.  
  157. # update the password for all env accounts or just one
  158. if run_all:
  159. # last chance to back out
  160. print 'Updating ALL accounts for %s: ' % user
  161. choice = raw_input('Are you sure (y/n)? ').lower()
  162. if 'y' in choice:
  163. change_all(accounts)
  164. else:
  165. print 'OK, bye!'
  166. sys.exit()
  167. elif user in accounts:
  168. env = get_env(user, accounts)
  169.  
  170. # give the user a final chance to change their mind
  171. print 'Updating account %s in %s: ' % (user, env)
  172. choice = raw_input('Are you sure (y/n)? ').lower()
  173. if 'y' in choice:
  174. update_pass(user, env)
  175. else:
  176. print 'OK, bye!'
  177. sys.exit()
  178. else:
  179. # user account specified was not found
  180. print '%s is not a valid account!' % user
  181. sys.exit()
  182. else:
  183. # point them in the right direction
  184. print usage
  185. sys.exit()
  186.  
  187.  
  188. if __name__ == '__main__':
  189. main(sys.argv[1:])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement