標籤:
關於linux下通過shell命令(自動)修改使用者密碼
2012-04-23 18:47:39
分類:
原文地址:關於linux下(自動)修改使用者密碼 ubuntuer
本文章總結了如何手動、自動修改本機使用者密碼及遠程機器的使用者密碼。對做自動化測試提供了自動修改使用者密碼的原理及方法。
修改本機使用者密碼:
1、互動配置本機使用者:
以root使用者:
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.
以非root使用者修改自己的密碼(注後面不能跟使用者名稱,只有root使用者才允許):
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、非互動配置本機使用者:
| echo <newpasswd> | passwd --stdin <username> |
或
| echo <username>:<passwd> | chpasswd |
或
將<username>:<passwd>對先寫到一檔案passwd.tmp中,然後執行
chpasswd < passwd.tmp
3、自動指令碼處理:
根據passwd命令修改使用者密碼,格式為:xxx.sh <username> <passwd>
#!/bin/sh
# \
exec expect -f "$0" ${1+"[email protected]"}
if { $argc != 2 } {
puts "Usage: $argv0 <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 |
說明:如果要通過shell直接調用expect相關命令,則開頭中必須是如下格式,然後後面即可按照expect、TCL格式書寫了。
#!/bin/sh
# \
exec expect -f "$0" ${1+"[email protected]"} |
根據echo <newpasswd> | passwd --stdin <username> 及 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 |
修改遠程主機上使用者密碼:
1、互動配置遠端使用者:
| echo <newpasswd> | ssh -l root <host> passwd --stdin <username> |
如:
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. |
或
| echo <username>:<passwd> | ssh -l root <host> chpasswd 2>&1 |
或
將<username>:<passwd>對先寫到一檔案passwd.tmp中,然後執行
chpasswd < passwd.tmp [作者未測試]
或
ssh -l root <host>
.... 互動輸入root密碼
然後執行以上的所有可用方式均可
2、非互動配置遠端使用者:
則需要用到expect來進行處理,通過ssh登入到遠程機器,然後結合上述配置方式,以完成自動修改使用者密碼。
#!/usr/bin/expect
#@brief to change user password by ssh remote machine
proc usage {funcname} {
puts "Usage: "
puts " $funcname <host> <username> <newpasswd> -user <userpasswd>"
puts " $funcname <host> <username> <newpasswd> -root <rootpasswd>"
}
# 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
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 是否將互動權接過來,如果接過來,則使用者這時可進行互動 |
[轉] 關於linux下通過shell命令(自動)修改使用者密碼