Linux 交互式和非交互式两种修改用户密码方法实例
发布:smiling 来源: PHP粉丝网 添加日期:2015-04-21 13:21:21 浏览: 评论:0
最近管理的一批机器,有个需求是要统一修改一个帐号的用户名密码,比如将qa帐号的密码改为1234,后来还为了脚本化,很方便的执行,还使用了非交互式地修改用户的密码,简单记录一下吧.
1.交互式配置本地用户的密码:passwd 命令,代码如下:
- [root@host_221-81 ~]# passwd qa
- Changing password for user qa.
- New password:
- BAD PASSWORD: it is too short
- BAD PASSWORD: is too simple
- Retype new password:
- passwd: all authentication tokens updated successfully.
2.非交互式修改本地用户的密码:chpasswd,代码如下:
- # chpasswd命令使用起来很简洁
- [root@host_221-81 ~]# echo "qa:1234" | chpasswd
- # 使用passwd命令,也可以实现非交互式修改密码
- [root@host_221-81 ~]# echo "1234" | passwd --stdin "qa"
- Changing password for user qa. --phpfensi.com
- passwd: all authentication tokens updated successfully.
3.使用expect来处理交互式输入,从而实现非交互式的密码修改,代码如下:
- #!/bin/sh
- #
- exec expect -f "$0" "$@"
- 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 "$passwordr"
- expect "assword:"
- send "$passwordr"
- expect eof
注意:脚本的第二行,这种写法可能比较陌生,这是在TCL语言中的语法,The backslash is recognized as part of a comment to sh, but in Tcl the backslash continues the comment into the next line which keeps the exec command from executing again.
该脚本的执行结果为:
- [root@smilejay ~]# ./change-pwd-expect.sh qa 1234
- spawn passwd qa
- Changing password for user qa.
- New password:
- BAD PASSWORD: it is too short
- BAD PASSWORD: is too simple
- Retype new password:
- passwd: all authentication tokens updated successfully.
Tags: Linux交互式 Linux修改密码
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)