Changing an AIX password via script?

2020-02-16 11:58发布

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 their password.

AIX is running on the system.

unfortunately, chpasswd is unavailable.

I have expected installed, but I am having trouble with that also.

here is what I thought would work

echo "oldpassword\nnewpasswd123\nnewpasswd123" | passwd user

However once run the script I am prompted with please enter user's old password shouldn't they all be echoed in?

I am a beginner with shell scripting and this has been baffled.

13条回答
甜甜的少女心
2楼-- · 2020-02-16 12:41

In addition to the other suggestions, you can also achieve this using a HEREDOC.

In your immediate case, this might look like:

$ /usr/bin/passwd root <<EOF
test
test
EOF
查看更多
Anthone
3楼-- · 2020-02-16 12:44

Use GNU passwd stdin flag.

From the man page:

   --stdin
          This option is used to indicate that passwd should read the new password from standard input, which can be a pipe.

NOTE: Only for root user.

Example

$ adduser foo 
$ echo "NewPass" |passwd foo --stdin
Changing password for user foo.
passwd: all authentication tokens updated successfully.

Alternatively you can use expect, this simple code will do the trick:

#!/usr/bin/expect
spawn passwd foo
expect "password:"
send "Xcv15kl\r"
expect "Retype new password:"
send "Xcv15kl\r"
interact

Results

$ ./passwd.xp 
spawn passwd foo
Changing password for user foo.
New password: 
Retype new password: 
passwd: all authentication tokens updated successfully.
查看更多
Animai°情兽
4楼-- · 2020-02-16 12:45

Just this

passwd <<EOF
oldpassword
newpassword
newpassword
EOF

Actual output from ubuntu machine (sorry no AIX available to me):

user@host:~$ passwd <<EOF
oldpassword
newpassword
newpassword
EOF

Changing password for user.
(current) UNIX password: Enter new UNIX password: Retype new UNIX password: 
passwd: password updated successfully
user@host:~$
查看更多
放荡不羁爱自由
5楼-- · 2020-02-16 12:45
#!/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()
查看更多
Lonely孤独者°
6楼-- · 2020-02-16 12:49

This is from : Script to change password on linux servers over ssh

The script below will need to be saved as a file (eg ./passwdWrapper) and made executable (chmod u+x ./passwdWrapper)

#!/usr/bin/expect -f
#wrapper to make passwd(1) be non-interactive
#username is passed as 1st arg, passwd as 2nd

set username [lindex $argv 0]
set password [lindex $argv 1]
set serverid [lindex $argv 2]
set newpassword [lindex $argv 3]

spawn ssh $serverid passwd
expect "assword:"
send "$password\r"
expect "UNIX password:"
send "$password\r"
expect "password:"
send "$newpassword\r"
expect "password:"
send "$newpassword\r"
expect eof

Then you can run ./passwdWrapper $user $password $server $newpassword which will actually change the password.

Note: This requires that you install expect on the machine from which you will be running the command. (sudo apt-get install expect) The script works on CentOS 5/6 and Ubuntu 14.04, but if the prompts in passwd change, you may have to tweak the expect lines.

查看更多
看我几分像从前
7楼-- · 2020-02-16 12:50

You can try

LINUX

echo password | passwd username --stdin

UNIX

echo username:password | chpasswd -c

If you dont use "-c" argument, you need to change password next time.

查看更多
登录 后发表回答