通常会有多台服务器需要同时修改密码,此时可不必一台一台去操作,可以借用expect工具实现批量密码修改工作。涉及到四个文件,ip地址列表文件(iplist.txt),远程密码修改脚本(password.sh),复制时调用密码脚本scp.exp,密码修改主程序(chpasswd.sh),需将四个文件放置在/root目录下,如果放在其它目录,需修改脚本中对应的路径

  1. 在执行脚本的机器上安装expect,使用rpm包安装时需要依赖tcl包,也可使用yum安装,使用mkpasswd生成密码,一次生成一次,可多次运行。

#mkpasswd -l 16 -s 3     #-l指定密码为16位,-s指定特殊字符为3
*Vdmz{u(2uF8jvnz

2.本地生成公钥和私钥

[root@localhost~]# ssh-keygen -t rsa
Generatingpublic/private rsa key pair.
Enterfile in which to save the key (/root/.ssh/id_rsa):
Enterpassphrase (empty for no passphrase):
Entersame passphrase again:
Youridentification has been saved in /root/.ssh/id_rsa.
Yourpublic key has been saved in /root/.ssh/id_rsa.pub.
The keyfingerprint is:
04:60:67:87:bb:5f:bc:2a:27:14:eb:90:c5:9c:54:46root@localhost.localdomain
The key'srandomart p_w_picpath is:
+--[ RSA2048]----+
|    o.++E       |
|   . oo+        |
|     + o.       |
|      B.        |
|     o +S.      |
|    o +  o      |
|     + . . .    |
|      + o .     |
|       +..      |
+-----------------+

3.定义修改密码的脚本password.sh,这个脚本是需要在远程机器上执行的,设置权限为700,此脚本中可定义一次性修改多个用户的密码,这里设置了root和guest,这里的密码是由密码生成工具mkpasswd生成的,需要记住此密码,脚本执行成功后,远程机器上即会自动修改成此密码。

#!/bin/bash#detectthe current user is root or not
if [ $UID-ne 0 ];thenecho "only root can run thisscript"exit 3
fiecho"*Vdmz{u(2uF8jvnz" | passwd --stdin root
if `id -uguest >/dev/null 2>&1`;thenecho "guest is already exist"echo "wifxg4hgla9ID@:?" |passwd --stdin guestecho "old guest's passwordchanged successful"
elseuseradd guestecho "user guest addedsuccessful"echo "wifxg4hgla9ID@:?" |passwd --stdin guestecho "guest's password changedsucessful"
fi

4.定义要修改的机器的列表iplist.txt,每行一个IP

192.168.18.131
192.168.18.132

5.自动输入密码并自动scp复制的脚本scp.exp,调用此脚本时,需指定源文件和目标文件两个参数。此脚本中的redhat为要修改机器的root原始密码,可在此处修改,要修改的多台机器原来必须是同样的root密码,否则无法完成一次性批量修改。

#!/usr/bin/expect
settimeout 20if {[llength $argv] < 2} {puts "Usage:"puts "$argv0 local_fileremote_path"exit 1
}setlocal_file [lindex $argv 0]
setremote_path [lindex $argv 1]
setpasswd redhat setpasswderror 0spawn scp$local_file $remote_pathexpect {"*assword:*" {if { $passwderror == 1 } {puts "passwd is error"exit 2}set timeout 1000set passwderror 1send "$passwd\r"exp_continue}"*es/no)?*" {send "yes\r"exp_continue}timeout {puts "connect is timeout"exit 3}
}

6.提供密码修改主程序chpass.sh

修改密码主程序chpass.sh,先将公钥id_rsa.pub和修改密码脚本password.sh上传至目标服务器上,执行修改密码脚本password.sh,执行完成后,删除password.sh

#!/bin/bash#changepassword for production system
#added bysunny 20160112
#mail:francis198@163.com#detectthe current user is root or not
if [ $UID-ne 0 ];thenecho "only root can run thisscript"exit 3
fi
#define aip address list
IPLIST=/root/iplist.txtfor i in`cat $IPLIST`do/root/scp.exp /root/.ssh/id_rsa.pubroot@$i:/root/.ssh/authorized_keys/root/scp.exp /root/password.shroot@$i:/root/password.shssh $i '/root/password.sh && rm-f /root/password.sh'done

7.执行修改密码脚本完成密码修改

执行过程中开启日志log功能,后续查看实施日志,对比修改状态

# ./chpass.sh
spawn scp/root/.ssh/id_rsa.pub root@192.168.18.131:/root/.ssh/authorized_keys
Theauthenticity of host '192.168.18.131 (192.168.18.131)' can't be established.
RSA keyfingerprint is d6:7b:b0:d8:2b:5f:90:9a:b4:97:c9:1f:dc:f7:44:8b.
Are yousure you want to continue connecting (yes/no)? yes
Warning:Permanently added '192.168.18.131' (RSA) to the list of known hosts.
root@192.168.18.131'spassword:
id_rsa.pub                                                          100%  396    0.4KB/s   00:00
spawn scp/root/password.sh root@192.168.18.131:/root/password.sh
password.sh                                                        100%  426     0.4KB/s  00:00
Changingpassword for user root.
passwd:all authentication tokens updated successfully.
guest isalready exist
Changingpassword for user guest.
passwd:all authentication tokens updated successfully.
oldguest's password changed successful

8.检查日志,过滤后查看密码修改状况

转载于:https://blog.51cto.com/francis198/1734901

自动批量修改linux用户密码相关推荐

  1. 批量修改linux服务器密码,Linux下批量修改服务器用户密码方法步骤

    Linux下批量修改服务器用户密码方法步骤 密码快要过期.由于机器数量众多,因此采用批量修改密码的方式来进行处理. 下面是具体步骤: 1.配置ssh免key # ssh-keygen -t rsa - ...

  2. Windows批量修改服务器密码,expect批量修改linux服务器密码

    expect批量修改linux服务器密码 内容精选 换一换 批量修改弹性云服务器信息.当前仅支持批量修改云服务器名称,一次最多可以修改1000台.PUT /v1/{project_id}/clouds ...

  3. 修改linux用户密码(passwd)

    对于初学者来说,如何修改linux用户密码也不是件容易的事,其实非常简单,下面举例说明: (1)修改root用户密码 如果是以root身份登录,修改root密码. 只要输入 passwd 就会出现: ...

  4. linux修改密码点点点root,linux系统批量修改root用户密码

    脚本目的:批量修改linux系统root用户密码 条件:必须是修改的用户是root,因为只有root才有权限使用passwd命令 说明:先把IP.用户.密码.端口信息写到old_info文件中,脚本从 ...

  5. ansible批量修改linux服务器密码的playbook

    从网上找到批量修改Linux服务器root密码的playbook. 使用方法: 1.输入要修改的inventory组 2.按需要,在playbook中输入要修改的IP.新密码,如下: - hosts: ...

  6. 使用chpasswd命令批量修改系统用户密码

    chpasswd命令工作原理: 从系统的标准输入读入用户的名称和口令,并利用这些信息来更新系统上已存在的用户的口令! 语法: 1:# echo 用户名:密码 | chpasswd 2:# chpass ...

  7. c修改linux 用户密码,Linux修改用户密码实用案例

    1. passwd命令简介 passwd命令用来更改Linux使用者的密码.passwd命令用于设置用户的认证信息,包括用户密码.密码过期时间等.系统管理者则能用它管理系统用户的密码.只有管理者可以指 ...

  8. 批量修改Linux系统密码

    截取主机IP地址最后一位数.匹配一组定义好的自定义密码.使用chpasswd修改主机账户密码 截图主机IP [root@web01 ~]# ip addr 1: lo: <LOOPBACK,UP ...

  9. expect 批量修改服务器用户密码

    每个技术人员离职,留下的人 就要修改他的服务器账号密码,很麻烦,故写次脚本偷懒 change.sh 如下 1 2 3 4 5 6 7 8 9 10 #!/bin/bash for i in `awk  ...

最新文章

  1. 使用余弦相似度算法计算文本相似度-数学
  2. 简书php硬件交互,php设计模式——适配器模式
  3. Python之tkinter:动态演示调用python库的tkinter带你进入GUI世界(Listbox/Scrollbar)
  4. 如何阅读《深入理解计算机系统》?(文末送书)
  5. python之布尔值——待补充……
  6. 整理与总结Python关于对文件的操作
  7. ospfdr选举规则_ospf DR和BDR选举注意的问题
  8. oracle恢复RAC到单机
  9. javascript 常用代码技巧大收集
  10. McaFee企业版v8.0i设置指南
  11. 小米html查看器闪退,小米手机浏览器闪退解决办法
  12. 说出来你可能不信,我用 Python 破解了微信聊天记录
  13. 中科院阿里云发布11比特云超导量子处理器
  14. 微信授权登陆服务器,微信公众号开发流程--微信第三方授权登陆流程
  15. matlab突然打不开,点击运行没有任何反应
  16. 图解:最短路径之迪杰斯特拉算法
  17. 量手知姓氏 :算命测姓氏 之 数学原理
  18. PHP裂变红包源码,php版本微信裂变红包api详解
  19. 舞蹈模特欣欣(六)棚拍私房 大家看看像小龙女(李若彤)吗?
  20. 【“玩物立志”-scratch少儿编程】亲手实现小猫走迷宫小游戏:其实挺简单

热门文章

  1. SpringData核心数据访问接口--CrudRepository示例
  2. 你只知道JVM栈,知不知道栈帧、局部变量表、slot、操作数栈?
  3. 独立测试团队在敏捷开发中的几个特别实践
  4. 1、SpringBoot整合JPA
  5. 地图画指定区域_零基础学CAD绘制一张桌子为例,使亲们更好地熟悉三维绘图环境...
  6. 证券业震荡,数字化智能化变革、升级与突破或是新生力量(附案例)
  7. Java Web之文件的上传及下载
  8. 不是你无法入门自然语言处理(NLP),而是你没找到正确的打开方式
  9. 从Http它被连接到WebSocket
  10. linux stack