This article summarizes how to manually and automatically modify the password of a local user and the password of a remote machine. The principles and methods for automatically changing user passwords are provided for automated testing.
Modify the password of a local user:
1. Interactive configuration of Local Users:
Root User:
Passwd <username>
Changing password for user dewang.
New UNIX password:
Bad password: it is too short
Retype new UNIX password:
Passwd: all authentication tokens updated successfully.
Change your password as a non-root user (only root users are allowed to change the password after the user name is added ):
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 to 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 of xxx. sh <username> <passwd>
#! /Bin/sh
#
Exec expect CT-f "$0" ${1 + "$ @"}
If {$ argc! = 2 }{
Puts "Usage: $ argv0 <username> <passwd>"
Exit 1
}
Set password [lindex $ argv 1]
Spawn passwd [lindex $ argv 0]
Sleep 1
Wrong CT "assword :"
Send "$ password"
Wrong CT "assword :"
Send "$ password"
CT eof
Note: If you want to use shell to directly call reverse CT-related commands, the start must be in the following format, and then you can follow the reverse CT and TCL formats.
#! /Bin/sh
#
Exec expect CT-f "$0" ${1 + "$ @"}
Modify the User Password according to echo <newpasswd> | passwd -- stdin <username> and echo <username >:< passwd> | chpasswd:
#! /Bin/sh
If [$ #-ne 2]; then
Echo "Usage: 'basename $ 0' <username> <passwd>"
Exit 1
Fi
# Echo "$2" | passwd -- stdin "$1"
Echo "$1: $2" | chpasswd
If [$? -Eq 0]; then
Echo "change password for $1 success"
Else
Echo "change password for $1 failed"
Fi
Modify the user password on the remote host:
1. Interactive Configuration Remote Users:
Echo <newpasswd> | ssh-l root For example:
Echo "newpass" | ssh-l root 10.11.103.151 passwd -- stdin dewang
A root@10.11.103.151s 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 to a file passwd. tmp, and then execute
Chpasswd <passwd. tmp [not tested by the author]
Or
Ssh-l root ... Enter the root password.
Then execute all the available methods above.
2. Remote users with non-interactive Configuration:
You need to use reverse CT for processing, log on to the remote machine through ssh, and then use the above configuration method to automatically modify the user password.
#! /Usr/bin/CT
# @ Brief to change user password by ssh remote machine
Proc usage {funcname }{
Puts "Usage :"
Puts "$ funcname Puts "$ funcname }
# Check param
If {$ argc! = 5 }{
Usage $ argv0
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
CT {
"* (Yes/no) *" {send "yes"; set sshkey 1}
"* Assword: *" {send "$ passwd"; set sshkey 0}
If sshkey = 1 {
Reset CT "* password :*"
Send "$ passwd"
}
}
CT "*#"
If {[string compare $ loginname "root"] = 0 }{
# Send "echo" $ username: $ newpasswd "| chpasswd"
Send "echo" $ newpasswd "| passwd -- stdin" $ username ""
} Else {
Send "passwd"
CT {
"* Current * assword:" {send "$ passwd "}
"Passwd: Authentication token manipulation error" {exit}
}
Wrong CT "New * assword :"
Send "$ newpasswd"
Wrong CT "Retype * assword :"
Send "$ newpasswd"
}
CT "*#"
Send "exit"
# Whether interact receives the interaction permission. If yes, the user can perform the interaction operation at this time.