bash 通过脚本更改 AIX 密码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27837674/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Changing an AIX password via script?
提问by Grushton94
I am trying to change a password of a user via script. I cannot use sudo as there is a feature that requires the user to change the password again if another user changes their password.
我正在尝试通过脚本更改用户的密码。我不能使用 sudo,因为如果另一个用户更改了他们的密码,有一项功能要求用户再次更改密码。
AIX is running on the system.
AIX 正在系统上运行。
unfortunately, chpasswd is unavailable.
不幸的是,chpasswd 不可用。
I have expected installed, but I am having trouble with that also.
我已经安装了,但我也遇到了问题。
here is what I thought would work
这是我认为可行的方法
echo "oldpassword\nnewpasswd123\nnewpasswd123" | passwd user
However once run the script I am prompted with please enter user's old password
shouldn't they all be echoed in?
但是,一旦运行我被提示的脚本,please enter user's old password
它们不应该都被回显吗?
I am a beginner with shell scripting and this has been baffled.
我是 shell 脚本的初学者,这一直很困惑。
回答by Mandar Shinde
You can try:
你可以试试:
echo "USERNAME:NEWPASSWORD" | chpasswd
echo "USERNAME:NEWPASSWORD" | chpasswd
回答by Juan Diego Godoy Robles
Use GNUpasswd
stdinflag.
使用GNUpasswd
标准输入标志。
From the man
page:
从man
页面:
--stdin This option is used to indicate that passwd should read the new password from standard input, which can be a pipe.
--stdin This option is used to indicate that passwd should read the new password from standard input, which can be a pipe.
NOTE:Only for rootuser.
注意:仅适用于root用户。
Example
例子
$ adduser foo
$ echo "NewPass" |passwd foo --stdin
Changing password for user foo.
passwd: all authentication tokens updated successfully.
Alternatively you can use expect
, this simple code will do the trick:
或者,您可以使用expect
,这个简单的代码可以解决问题:
#!/usr/bin/expect
spawn passwd foo
expect "password:"
send "Xcv15kl\r"
expect "Retype new password:"
send "Xcv15kl\r"
interact
Results
结果
$ ./passwd.xp
spawn passwd foo
Changing password for user foo.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.
回答by Andrew Yasinsky
回答by Elliot Beaumont
You need echo -e for the newline characters to take affect
您需要 echo -e 使换行符生效
you wrote
你写了
echo "oldpassword\nnewpasswd123\nnewpasswd123" | passwd user
you should try
你应该试试
echo -e "oldpassword\nnewpasswd123\nnewpasswd123" | passwd user
more than likely, you will not need the oldpassword\n portion of that command, you should just need the two new passwords. Don't forget to use single quotes around exclamation points!
很可能,您不需要该命令的 oldpassword\n 部分,您只需要两个新密码。不要忘记在感叹号周围使用单引号!
echo -e "new"'!'"passwd123\nnew"'!'"passwd123" | passwd user
回答by CoreyJJohnson
Just this
只是这个
passwd <<EOF
oldpassword
newpassword
newpassword
EOF
Actual output from ubuntu machine (sorry no AIX available to me):
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:~$
回答by TehesFR
You can try :
你可以试试 :
echo -e "newpasswd123\nnnewpasswd123" | passwd user
echo -e "newpasswd123\nnnewpasswd123" | 密码用户
回答by Loren
This is from : Script to change password on linux servers over ssh
这是来自:通过 ssh 在 linux 服务器上更改密码的脚本
The script below will need to be saved as a file (eg ./passwdWrapper
) and made executable (chmod u+x ./passwdWrapper)
下面的脚本需要保存为文件(例如./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
Then you can run ./passwdWrapper $user $password $server $newpassword
which will actually change the password.
然后你可以运行./passwdWrapper $user $password $server $newpassword
这将实际更改密码。
Note: This requires that you install expect
on the machine from which you will be running the command. (sudo apt-get install expect
) The script works on CentOS 5/6 and Ubuntu 14.04, but if the prompts in passwd
change, you may have to tweak the expect
lines.
注意:这要求您安装expect
在将运行命令的机器上。( sudo apt-get install expect
) 该脚本适用于 CentOS 5/6 和 Ubuntu 14.04,但如果提示发生passwd
变化,您可能需要调整expect
行。
回答by Ahmet Mecid Kaya
You can try
你可以试试
LINUX
LINUX
echo password | passwd username --stdin
回显密码 | passwd 用户名 --stdin
UNIX
UNIX
echo username:password | chpasswd -c
回显用户名:密码 | chpasswd -c
If you dont use "-c" argument, you need to change password next time.
如果不使用“-c”参数,下次需要更改密码。
回答by Ikruzzz
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
Refer the link below as well...
也可以参考下面的链接...
http://www.putorius.net/2013/04/bash-script-to-change-users-password.html
回答by itengineer
If you can use ansible, and set the sudo rights in it, then you can easily use this script. If you're wanting to script something like this, it means you need to do it on more than one system. Therefore, you should try to automate that as well.
如果您可以使用ansible,并在其中设置sudo权限,那么您可以轻松使用此脚本。如果您想编写这样的脚本,则意味着您需要在多个系统上执行此操作。因此,您也应该尝试将其自动化。