• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >linux shell > Linux下交互式与非交互式修改用户密码的例子

Linux下交互式与非交互式修改用户密码的例子

作者:junjie 字体:[增加 减小] 来源:互联网

junjie 通过本文主要向大家介绍了linux交互式,linux 交互,linux 交互命令,linux shell 交互,linux makefile例子等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

最近管理的一批机器,有个需求是要统一修改一个帐号的用户名密码,比如将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.
</div>

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.
passwd: all authentication tokens updated successfully.
</div>

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 "$password\r"
expect "assword:"
send "$password\r"
expect eof
</div>

注意:脚本的第二行,这种写法可能比较陌生,这是在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.
</div>

</div>

您可能想查找下面的文章:

  • Linux下交互式与非交互式修改用户密码的例子

相关文章

  • 批量修改ssh配置的expect脚本
  • linux系统安装字体详细介绍
  • shell版Nginx日志蜘蛛爬取查看脚本
  • linux下实现ftp自动备份shell脚本
  • Linux在shell中自动生成1到100的数组方法(两种方法)
  • Linux下JDK中文字体乱码的解决方法
  • Shell脚本IF条件判断和判断条件总结
  • shell实现自动adsl拨号并检测连接状况脚本分享
  • Shell脚本实现检测进程是否正在运行
  • awk统计文件中某关键词出现次数的命令

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • Shell脚本中判断输入参数个数的方法
    • linux中Jetty的安装和配置方法
    • 通过shell进行数学运算的多种方式
    • Bash中数组的操作教程
    • expect实现单台、多台服务器批量scp传输文件
    • linux shell发送Email邮件的方法详解
    • linux shell实现判断输入的数字是否为合理的浮点数
    • Shell脚本统计文件行数的8种方法
    • 通过短信发送LOG归类号码发送情况的shell脚本
    • vim快捷键大全

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有