Changing an AIX password via script?

后端 未结 13 1929
無奈伤痛
無奈伤痛 2020-12-23 16:43

I am trying to change a password of a user via script. I cannot use sudo as there is a feature that requires the user to change the password again if another user changes th

13条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-23 17:24

    #!/usr/bin/python
    
    import random
    import string
    import smtplib
    import sys
    import os
    from subprocess import call
    import socket
    
    user = sys.argv[1]
    receivers = ["%s@domain.com" %user]
    
    '''This will generate a 30 character random password'''
    def genrandpwd():
            return  ''.join(random.SystemRandom().choice(string.ascii_lowercase + string.digits + string.ascii_uppercase + string.punctuation) for _ in range(30))
    
    def change_passwd(user, password):
            p = os.popen("/usr/bin/passwd %s" %user, "w")
            p.write(password)
            p.write("\n")
            p.write(password)
        p.close()
    
    def chage(user):
            agepasswd = call(["/usr/bin/chage", "-d", "0", "%s" %user])
    
    def mailpwd(user, password):
            sender = "admin@%s" %socket.gethostname()
            subj = "!!!IMPORTANT!!!, Unix password changed for user %s" %user
            text = "The password for the %s user has changed, the new password is:\n\n %s \n\n Note: The system will force to change the password upon initial login. Please use the password provided in the mail as your current password and type the password of your choice as the New password" %(user, password)
            message = message = 'Subject: %s\n\n%s' % (subj, text)
            smtpObj = smtplib.SMTP('mailrelay-server.domain.com')
            smtpObj.sendmail(sender, receivers, message)
            smtpObj.quit()
    
    def main():
            newpwd = genrandpwd()
            change_passwd(user, newpwd)
            chage(user)
            mailpwd(user, newpwd)
    
    if __name__ == "__main__":
            main()
    

提交回复
热议问题