[Go] about Linux under the shell Command (automatic) to modify the user password

Source: Internet
Author: User
Tags stdin

About Linux under the shell Command (automatic) to modify the user password

2012-04-23 18:47:39

Classification:

Original address: About Linux under (automatic) modify user password Ubuntuer

This article summarizes how to manually and automatically modify the local user password and the remote machine's user password. The principle and method of automatically modifying the user's password are provided for automated testing.

To modify the local user password:
1. Configure Local Users interactively:
As Root User:
passwd <username>
Changing password for user Dewang.
New UNIX Password:
Bad password:it are too short
Retype new UNIX Password:
Passwd:all authentication tokens updated successfully.
Change your password with a non-root user (note that the user name is not followed, only the root user is allowed):
passwd
Changing password for user Dewang.
changing password for Dewang
(current) UNIX Password:
New UNIX Password:
Retype new UNIX Password:
Passwd:all authentication tokens updated successfully.

2. Non-interactive configuration of Local Users:
echo <newpasswd> | passwd--stdin <username>

or
echo <username>:<passwd> | chpasswd

or
Write the <username>:<passwd> pair first into a file passwd.tmp, and then execute
CHPASSWD < Passwd.tmp

3, Automatic Script processing:
Modify the user password according to the passwd command in the format: xxx.sh <username> <passwd>
#!/bin/sh
# \
EXEC expect-f "$" ${1+ "[email protected]"}
if {$ARGC! = 2} {
Puts "Usage: $argv 0 <username> <passwd>"
Exit 1
}
Set password [lindex $argv 1]
Spawn passwd [lindex $argv 0]
Sleep 1
Expect "Assword:"
Send "$password \ r"
Expect "Assword:"
Send "$password \ r"
Expect EOF

Note: If you want to call expect related commands directly through the shell, you must start with the following format, and then you can follow the expect, TCL format.
#!/bin/sh
# \
EXEC expect-f "$" ${1+ "[email protected]"}

According to echo <newpasswd> | passwd--stdin <username> and echo <username>:<passwd> | CHPASSWD to modify the user password:
#!/bin/sh

If [$#-ne 2]; Then
echo "Usage: ' basename $ <username> <passwd>"
Exit 1
Fi

#echo "$" | passwd--stdin "$"
echo "$1:$2" | chpasswd
If [$?-eq 0]; Then
echo "Change password for $ success"
Else
echo "Change password for $ failed"
Fi


To Modify the user password on the remote host:
1. Interactively configure remote users:
echo <newpasswd> | Ssh-l root

such as:
echo "Newpass" | Ssh-l root 10.11.103.151 passwd--stdin Dewang
[email protected] ' s password:
Changing password for user Dewang.
Passwd:all authentication tokens updated successfully.

or
echo <username>:<passwd> | Ssh-l Root

or
Write the <username>:<passwd> pair first into a file passwd.tmp, and then execute
CHPASSWD < passwd.tmp [Author not tested]
Or
Ssh-l Root .... Interactive input root password
Then perform all of the above available methods to

2. Non-interactive configuration of remote users:
You need to use expect for processing, SSH login to the remote machine, and then combined with the above configuration to complete the automatic user password modification.
#!/usr/bin/expect
# @brief to the change user password by SSH remote

Proc Usage {funcname} {
Puts "Usage:"
Puts "$funcname Puts "$funcname }

# check Param
if {$ARGC! = 5} {
Usage $ARGV 0
Exit 1
}

# get Param
Set host [lindex $ARGV 0]
Set username [lindex $argv 1]
Set NEWPASSWD [lindex $argv 2]
Set LoginName "Root"
if {[string compare [Lindex $argv 3] "-user"] = = 0} {
Set LoginName $username
}
Set passwd [lindex $argv 4]
Puts "$host $username $newpasswd $loginname $passwd"

Spawn Ssh-l $loginname $host
Expect {
"* (yes/no) *" {send "yes\r"; set Sshkey 1}
"*assword:*" {send "$passwd \ r"; Set Sshkey 0}
if Sshkey = = 1 {
Expect "*password:*"
Send "$passwd \ r"
}
}
Expect "*#"

if {[string compare $loginname "root"] = = 0} {
#send "echo \" $username: $newpasswd \ "| Chpasswd\r "
Send "echo \" $newpasswd \ "| passwd--stdin \ "$username \" \ r "
} else {
Send "passwd\r"
Expect {
"*current*assword:" {send "$passwd \ r"}
"Passwd:authentication Token manipulation error" {exit}
}
Expect "New*assword:"
Send "$NEWPASSWD \ r"
Expect "Retype*assword:"
Send "$NEWPASSWD \ r"
}
Expect "*#"
Send "exit\r"
#interact whether to take the right to interact, and if so, the user can then interact

[Go] about Linux under the shell Command (automatic) to modify the user password

Related Article

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.