Linux Interactive and non-interactive Two examples of modifying user password methods

Source: Internet
Author: User

Recently managed a number of machines, there is a need to unify the change of an account username password, such as the QA account password changed to 1234, and later for scripting, very convenient implementation, but also used to modify the user's password non-interactive. Simply record it.

1. Interactively configure the local user's password: passwd command

The code is as follows Copy Code
[Root@host_221-81 ~]# passwd QA
Changing password for user QA.
New Password:
Bad password:it are too short
Bad Password:is too simple
Retype new Password:
Passwd:all authentication tokens updated successfully.



2. Non-interactive change password for local User: CHPASSWD

The code is as follows Copy Code
# CHPASSWD commands are simple to use
[Root@host_221-81 ~]# echo "qa:1234" | chpasswd

# using the passwd command, you can also implement a non-interactive password change
[Root@host_221-81 ~]# echo "1234" | passwd--stdin "QA"
Changing password for user QA.
Passwd:all authentication tokens updated successfully.



3. Use expect to process interactive input, which enables non-interactive password modifications.

  code is as follows copy code

#!/bin/sh
#
Exec expect-f "$" "$@"
If {$ARGC!= 2} {
    puts "Usage: $ARGV 0 <username> & Lt;passwd> "
    exit 1
}
Set password [lindex $argv 1]
Spawn passwd [lindex $argv 0]
Sleep 1
Expect "Assword:"
Send "$passwordr"
Expect "Assword:"
Send "$passwordr"
expect EOF



Note: The second line of the script, which may be unfamiliar, is the syntax in the TCL language, the backslash is recognized as part of a comment to sh, but in Tcl the backslash Continu Es the comment into the next line which keeps the EXEC command from executing again.

The execution result of the script is:

The code is as follows Copy Code
[Root@smilejay ~]#./change-pwd-expect.sh QA 1234
Spawn passwd QA
Changing password for user QA.
New Password:
Bad password:it are too short
Bad Password:is too simple
Retype new Password:
Passwd:all authentication tokens updated successfully.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.