使用passwd命令从shell脚本中 [英] using the passwd command from within a shell script

查看:462
本文介绍了使用passwd命令从shell脚本中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写一个shell脚本来自动添加一个新用户,并更新自己的密码。我不知道如何获取密码从shell脚本,而不是交互提示我新的密码读取。我的code如下。

的adduser $ 1
passwd文件$ 1
$ 2
$ 2


解决方案

男子1 passwd文件

  --stdin
          这个选项被用来指示passwd中应读出的新
          从标准输入,它可以是一个管密码。

所以你的情况

 的adduser$ 1
回声$ 2| passwd文件$ 1--stdin

[更新]的几个问题进行了评论提出了:

passwd文件命令可能不会有一个 - 标准输入选项:使用 chpasswd的
实用,而不是按照建议由<一个href=\"http://stackoverflow.com/questions/714915/using-the-passwd-command-from-within-a-shell-script/715802#715802\">ashawley.

如果您使用bash的比其他的壳,回声可能不是一个内置命令,
和外壳将调用 /斌/回声。这是不安全的,因为密码
将在进程表中显示出来,并可以像 PS 工具中可以看出。

在这种情况下,你应该使用其他脚本语言。下面是Perl中的例子:

 #!的/ usr / bin中/ perl的-w
打开我的$管,'| chpasswd的'或死亡:;不能打开管道$!
打印{$}管$用户名:密码$;
关闭$管

I'm writing a shell script to automatically add a new user and update their password. I don't know how to get passwd to read from the shell script instead of interactively prompting me for the new password. My code is below.

adduser $1
passwd $1
$2
$2

解决方案

from "man 1 passwd":

   --stdin
          This option is used to indicate that passwd should read the new
          password from standard input, which can be a pipe.

So in your case

adduser "$1"
echo "$2" | passwd "$1" --stdin

[Update] a few issues were brought up in the comments:

Your passwd command may not have a --stdin option: use the chpasswd utility instead, as suggested by ashawley.

If you use a shell other than bash, "echo" might not be a builtin command, and the shell will call /bin/echo. This is insecure because the password will show up in the process table and can be seen with tools like ps.

In this case, you should use another scripting language. Here is an example in Perl:

#!/usr/bin/perl -w
open my $pipe, '|chpasswd' or die "can't open pipe: $!";
print {$pipe} "$username:$password";
close $pipe

这篇关于使用passwd命令从shell脚本中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆