Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- ###################################################
- # File name: update_passwords.py #
- # Author: Martin Uribe #
- # Date created: 03/17/2017 #
- # Date last modified: 03/23/2017 #
- # Python Version: 2.6.5 #
- ###################################################
- """Script for updating the passwords of RIDS user accounts.
- You have the option of updating the passwords for all of your RIDS accounts.
- This incules both the UNIX and RIDS passwords. The changes are pushed out
- for each environment, which takes time so make sure that you actually want
- to update them all.
- Optionally you have the choice of updating a single account. You must
- specify the user that you want to update.
- """
- import getopt
- import os
- import sys
- __version__ = '1.0.1'
- __author__ = 'Martin Uribe'
- __email__ = 'martin.uribe AT mywork.com'
- def change_all(accounts):
- """Used for updating all of your RIDS accounts at once.
- This function calls update_pass() while looping through all of your RIDS
- accounts.
- :param accounts: List of all of your accounts on the system
- :return: None
- """
- for account in accounts:
- env = get_env(account, accounts)
- update_pass(account, env)
- def update_pass(user, env):
- """Used to make the actual changes to your accounts.
- It will run the UNIX passwd command against the user account. It will
- then change its working directory to that of the environment to which
- the account belongs to and push out the updated password with the
- user_unix2rids perl script.
- :param user: String containing the user account to modify
- :param env: String containing the environment that the account belongs to
- :return: None
- """
- push_update = 'sr -u apache ./user_unix2rids -update %s' % user
- change_passwd = 'sr passwd %s' % user
- env_path = '/usr/' + env + '/bin'
- if os.path.isdir(env_path):
- os.chdir(env_path)
- print '[%s] %s:' % (env.upper(), user)
- # actually modify the system
- os.system(change_passwd)
- os.system(push_update)
- else:
- print '%s does not exist!' % env.upper()
- def get_accounts(user):
- """Used to get all of your the RIDS accounts.
- These accounts are based on the account from which you are currently
- logged in with or the account that you specified at the command line
- with the --user flag.
- :param user: String containing the user account to modify
- :return: Dictionary of user accounts associated with the user provided
- """
- with open('/etc/passwd', 'r') as p:
- # just had to play around with dictionary comprehensions :D
- accounts = dict((line.split(':')[0], line.split(':')[-1].split('/')[2].strip()) for line in p if user in line)
- return accounts
- def get_env(user, accounts):
- """Used to get the environment of the account.
- I've made the assumption here that any account that doesn't have
- startExt as its shell is an env1 account for the Field Engineer,
- who is the one most likely to be using this script.
- :param user: String containing the user account to look up
- :param accounts: Dictionary of accounts used to look-up the env for the account
- :return: String of the environment that the account belongs to
- """
- env = accounts[user]
- if 'env' not in env:
- env = 'env1'
- return env
- def info(user):
- """Used to display information on how to run this program
- Displays a more detailed explanation of how to run the script and
- details all of the flags that it can accept.
- :return: None
- """
- print '\nThis script will change the passwords for the given user account(s)'
- print 'on the system. If an optional argument is used, both the user and the'
- print 'env options must be provided.\n'
- print ' -h, --help: This help info'
- print ' -a, --all : Make changes to all of your accounts'
- print ' -u, --user: The username of the account that you want to modify'
- print '\n examples: ' + sys.argv[0] + ' -u ' + user
- print ' ' + sys.argv[0] + ' --user ' + user
- def main(argv):
- """Main entry point to the program
- This function puts it all together.
- :param argv: List of arguments that were past in when the script was ran
- :return: None
- """
- import getpass
- usage = 'USAGE: ' + sys.argv[0] + ' --help --all [--user <user>]'
- user = getpass.getuser()
- accounts = get_accounts(user)
- flags = len(argv)
- run_all = False
- if 0 <= flags <= 2:
- try:
- opts, args = getopt.getopt(argv, 'hau:', ['help', 'all', 'user='])
- except getopt.GetoptError:
- # some funky stuff was put in, get out now!
- print 'Invalid argument used!'
- print usage
- sys.exit(2)
- # cycle through the options
- for opt, arg in opts:
- if opt in ('-h', '--help'):
- print usage
- info(user)
- sys.exit()
- elif opt in ('-a', '--all'):
- run_all = True
- elif opt in ('-u', '--user'):
- user = arg
- # update the password for all env accounts or just one
- if run_all:
- # last chance to back out
- print 'Updating ALL accounts for %s: ' % user
- choice = raw_input('Are you sure (y/n)? ').lower()
- if 'y' in choice:
- change_all(accounts)
- else:
- print 'OK, bye!'
- sys.exit()
- elif user in accounts:
- env = get_env(user, accounts)
- # give the user a final chance to change their mind
- print 'Updating account %s in %s: ' % (user, env)
- choice = raw_input('Are you sure (y/n)? ').lower()
- if 'y' in choice:
- update_pass(user, env)
- else:
- print 'OK, bye!'
- sys.exit()
- else:
- # user account specified was not found
- print '%s is not a valid account!' % user
- sys.exit()
- else:
- # point them in the right direction
- print usage
- sys.exit()
- if __name__ == '__main__':
- main(sys.argv[1:])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement