expect怎么实现批量修改linux密码

这篇文章主要讲解了“expect怎么实现批量修改linux密码”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“expect怎么实现批量修改linux密码”吧!

最近对linux批量执行的脚本很感兴趣,在网上到处找有关expect批量执行脚本,今天就给大家共享一个批量修改密码的脚本.

脚本内容:

代码如下:

#!/usr/bin/expect
if { $argc<2 } {
    send_user "usage: $argv0 <host file> <cmd file> \n"
    exit
}
 
# 机器列表数据格式:  IP  端口  旧密码  新密码
set hostfile    [ open [lindex $argv 0] ]
# 命令列表数据格式:  一条命令一行
set cmdfile    [ open [lindex $argv 1] ]
 
# 数据文件分割符,默认为空格
set part "\ "
 
# 过滤关键字
set key_password "password:\ "
set key_init "\(yes/no\)\?\ "
set key_confirm "'yes'\ or\ 'no':\ "
set key_ps "*]#\ "
set key_newpassword "UNIX password:\ "
set timeout 30
 
log_file ./exprct.log
match_max 20480
 
while {[gets $hostfile _hosts_] >= 0} {
    set hosts [string trim $_hosts_]
    set str_index [string first $part $hosts]
    set host [string trim [string range $hosts 0 $str_index]]
    set temp [string trim [string range $hosts [expr $str_index + 1] [string length $hosts]]]
    set str_index [string first $part $temp]
 
    if { $str_index == -1 } {
        set port 22
        set pass $temp
        set newpass $temp
    } else {
        set port [string trim [string range $temp 0 $str_index]]
        set temp_pass [string trim [string range $temp [expr $str_index + 1] [string length $temp]]]
        set str_index [string first $part $temp_pass]
        set pass [string trim [string range $temp_pass 0 $str_index]]
        set newpass [string trim [string range $temp_pass [expr $str_index + 1] [string length $temp_pass]]]
    }
 
    spawn ssh -p $port $host
    while {1} {
        expect {
            "$key_password" {
                send "$pass\r"
            }
            "$key_init" {
                send "yes\r"
            }
            "$key_confirm" {
                send "yes\r"
            }
            "$key_ps" {
                while {[gets $cmdfile cmd] >= 0} {
                    send "$cmd\r"
                    expect {
                        "$key_ps" {
                            continue
                        }
                        "$key_newpassword" {
                            send "$newpass\r"
                            expect "$key_newpassword" {
                                send "$newpass\r"
                                expect "$key_ps"
                                continue
                            }
                        }
                    }
                }
                seek $cmdfile 0 start
                send_user "\r"
                break
            }
            timeout {
                puts "$host timeout\n"
                break
            }
        }
    }
    send "exit\r"
    close
    wait
}
 
close $hostfile
close $cmdfile
 
exit

感谢各位的阅读,以上就是“expect怎么实现批量修改linux密码”的内容了,经过本文的学习后,相信大家对expect怎么实现批量修改linux密码这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是本站,小编将为大家推送更多相关知识点的文章,欢迎关注!

相关推荐:

如何卸载Linux Compton

在Linux上卸载Compton(或它的常见继任者picom)取决于你当初是如何安装的。下面按常见情况分别说明。 一、先确认你装的是哪个 很多系统里“compton”实际是picom(compton已停止维护): whichcompton whichpicom 或者: compton--versio...

怎样优化Compton性能

Compton是Xorg下的轻量级合成管理器(现在多被picom取代),优化它的性能可以从配置、渲染后端、特效、硬件加速、系统环境等方面入手。下面按实用程度给你一套优化思路。 一、优先建议:使用picom而不是旧版Compton 旧版Compton已停止维护,picom是继任者,性能和兼容性更好: ...

linux hostname是什么

在Linux中,hostname(主机名)是用来标识一台计算机在网络中的名称。 一、hostname是什么? hostname是Linux系统的一个字符串标识 用于: 本地系统识别 网络通信(如SSH、局域网) 日志记录 通常是一个简短的名字,例如:myserver dev-machine loca...

Compton如何设置透明

在Compton(现在多指picom的前身)里设置透明,主要有几种方式,取决于你想让窗口透明还是桌面/背景透明。下面按常见场景说明。 一、确认你用的是Compton还是Picom 很多发行版已经用picom代替compton: whichcompton whichpicom 有picom→用pico...

linux hostname怎么设置

在Linux中设置hostname(主机名)有多种方式,取决于你用的是临时设置还是永久设置,以及发行版(systemd/非systemd)。下面按常见情况说明。 一、临时设置(重启后失效) sudohostname新主机名 示例: sudohostnamemyserver ⚠️只影响当前运行环境,重...

Compton能替代Picom吗

简短回答:可以替代,但要看你的使用场景和需求。 下面分情况说明: 1.Compton与Picom的关系 Compton是早期的一个X11合成管理器(compositor),现在已经基本停止维护。 Picom是Compton的一个活跃分支(fork),可以理解为: Picom=Compton的继任者 ...

linux hostname在哪查看

在Linux中,查看hostname(主机名)有以下几种常用方法: 1️⃣直接查看当前主机名 hostname 或 cat/etc/hostname 2️⃣使用hostnamectl(推荐,适用于systemd系统) hostnamectl 输出示例: Statichostname:myserver...

怎样解决Compton黑屏

Compton是Xorg下的一个老牌合成器(compositor),现在多数发行版已改用picom(Compton的分支)。出现“黑屏”通常不是显卡坏了,而是合成器参数、显卡驱动或OpenGL渲染方式不匹配。下面按最常见→较少见的顺序排查。 一、先确认你用的是Compton还是Picom which...