我正在尝试通过脚本更改用户的密码。我不能使用sudo,因为有一项功能要求用户在其他用户更改密码时再次更改密码。
AIX正在系统上运行。
不幸的是,chpasswd无法使用。
我原本预计会安装,但我也遇到了麻烦。
这是我认为可行的方法
echo "oldpassword\nnewpasswd123\nnewpasswd123" | passwd user
然而,一旦运行脚本,我就会被please enter user's old password
提示
不应该全部回应吗?
我是shell脚本的初学者,这令人困惑。
答案 0 :(得分:75)
您可以尝试:
echo "USERNAME:NEWPASSWORD" | chpasswd
答案 1 :(得分:21)
使用 GNU passwd
stdin 标记。
来自man
页面:
--stdin This option is used to indicate that passwd should read the new password from standard input, which can be a pipe.
注意:仅适用于 root 用户。
示例强>
$ adduser foo
$ echo "NewPass" |passwd foo --stdin
Changing password for user foo.
passwd: all authentication tokens updated successfully.
或者您可以使用expect
,这个简单的代码可以解决问题:
#!/usr/bin/expect
spawn passwd foo
expect "password:"
send "Xcv15kl\r"
expect "Retype new password:"
send "Xcv15kl\r"
interact
<强>结果
$ ./passwd.xp
spawn passwd foo
Changing password for user foo.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.
答案 2 :(得分:7)
答案 3 :(得分:6)
你需要echo -e才能使换行符符号生效
你写了echo "oldpassword\nnewpasswd123\nnewpasswd123" | passwd user
你应该试试
echo -e "oldpassword\nnewpasswd123\nnewpasswd123" | passwd user
很可能,您不需要该命令的oldpassword \ n部分,您应该只需要两个新密码。别忘了在感叹号周围使用单引号!
echo -e "new"'!'"passwd123\nnew"'!'"passwd123" | passwd user
答案 4 :(得分:3)
您可以尝试:
echo -e“newpasswd123 \ nnnewpasswd123”| passwd用户
答案 5 :(得分:3)
就是这个
passwd <<EOF
oldpassword
newpassword
newpassword
EOF
ubuntu机器的实际输出(抱歉没有AIX可供我使用):
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:~$
答案 6 :(得分:2)
来自:Script to change password on linux servers over ssh
以下脚本需要保存为文件(例如./passwdWrapper
)并可执行(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
然后您可以运行实际更改密码的./passwdWrapper $user $password $server $newpassword
。
注意:这要求您在运行命令的计算机上安装expect
。 (sudo apt-get install expect
)该脚本适用于CentOS 5/6和Ubuntu 14.04,但如果passwd
中的提示发生变化,则可能需要调整expect
行。
答案 7 :(得分:1)
Here is the script...
#!/bin/bash
echo "Please enter username:"
read username
echo "Please enter the new password:"
read -s password1
echo "Please repeat the new password:"
read -s password2
# Check both passwords match
if [ $password1 != $password2 ]; then
echo "Passwords do not match"
exit
fi
# Does User exist?
id $username &> /dev/null
if [ $? -eq 0 ]; then
echo "$username exists... changing password."
else
echo "$username does not exist - Password could not be updated for $username"; exit
fi
# Change password
echo -e "$password1\n$password1" | passwd $username
请参阅以下链接......
http://www.putorius.net/2013/04/bash-script-to-change-users-password.html
答案 8 :(得分:1)
你可以尝试
<强> LINUX 强>
回显密码| passwd用户名--stdin
<强> UNIX 强>
echo用户名:密码| chpasswd -c
如果你不使用&#34; -c&#34;参数,你需要在下次更改密码。
答案 9 :(得分:0)
对我而言,这适用于流浪汉VM:
sudo /usr/bin/passwd root <<EOF
12345678
12345678
EOF
答案 10 :(得分:0)
如果您可以使用ansible,并在其中设置sudo权限,那么您可以轻松使用此脚本。如果您想要编写类似这样的脚本,则意味着您需要在多个系统上执行此操作。因此,您也应该尝试自动化。
答案 11 :(得分:-1)
printf "oldpassword/nnewpassword/nnewpassword" | passwd user
答案 12 :(得分:-2)
#!/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()