北京SEO

Linux 交互式和非交互式两种修改用户密码方法实例

2019/10/10/17:46:40  阅读:1907  来源:谷歌SEO算法  标签: 百度K站

最近管理的一批机器,有个需求是要统一修改一个帐号的用户名密码,比如将qa帐号的密码改为1234,后来还为了脚本化,很方便的执行,还使用了非交互式地修改用户的密码,简单记录一下吧.

1.交互式配置本地用户的密码:passwd 命令,代码如下:

  1. [root@host_221-81~]#passwdqa
  2. Changingpasswordforuserqa.
  3. Newpassword:
  4. BADPASSWORD:itistooshort
  5. BADPASSWORD:istoosimple
  6. Retypenewpassword:
  7. passwd:allauthenticationtokensupdatedsuccessfully.

2.非交互式修改本地用户的密码:chpasswd,代码如下:

  1. #chpasswd命令使用起来很简洁
  2. [root@host_221-81~]#echo"qa:1234"|chpasswd
  3. #使用passwd命令,也可以实现非交互式修改密码
  4. [root@host_221-81~]#echo"1234"|passwd--stdin"qa"
  5. Changingpasswordforuserqa.--phpfensi.com
  6. passwd:allauthenticationtokensupdatedsuccessfully.

3.使用expect来处理交互式输入,从而实现非交互式的密码修改,代码如下:

  1. #!/bin/sh
  2. #
  3. execexpect-f"$0""$@"
  4. if{$argc!=2}{
  5. puts"Usage:$argv0<username><passwd>"
  6. exit1
  7. }
  8. setpassword[lindex$argv1]
  9. spawnpasswd[lindex$argv0]
  10. sleep1
  11. expect"assword:"
  12. send"$passwordr"
  13. expect"assword:"
  14. send"$passwordr"
  15. expecteof

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

该脚本的执行结果为:

  1. [root@smilejay~]#./change-pwd-expect.shqa1234
  2. spawnpasswdqa
  3. Changingpasswordforuserqa.
  4. Newpassword:
  5. BADPASSWORD:itistooshort
  6. BADPASSWORD:istoosimple
  7. Retypenewpassword:
  8. passwd:allauthenticationtokensupdatedsuccessfully.

广告内容

Linux 交互式和非交互式两种修改用户密码方法实例 Linux 交互式和非交互式两种修改用户密码方法实例 Linux 交互式和非交互式两种修改用户密码方法实例

相关阅读

热门评论

小浪SEO博客 小浪SEO博客

专注SEO优化思维、SEO技术的实战分享类博客~

总篇数183

精选文章

RMAN中catalog和nocatalog区别介绍 小技巧:为Linux下的文件分配多个权限 zimbra8.5.1安装第三方签名ssl证书的步骤 解决mysql不能远程连接数据库方法 windows服务器mysql增量备份批处理数据库 mysql中slow query log慢日志查询分析 JavaScript跨域问题总结 Linux下负载均衡软件LVS配置(VS/DR)教程 mysql中权限参数说明 MYSQL(错误1053)无法正常启动

SEO最新算法