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

expect实现批量修改linux密码脚本分享

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

junjie 通过本文主要向大家介绍了linux expect 脚本,linux expect,linux expect的用法,linux expect安装,linux expect命令等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

最近对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
</div>

</div>

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

  • expect实现批量修改linux密码脚本分享
  • 批量修改ssh配置的expect脚本
  • ssh expect自动登录的脚本代码

相关文章

  • 输出执行操作和打印日志的shell脚本实例
  • Shell中的${}、##和%%使用范例
  • Linux查找占用的端口,并杀死进程的方法
  • 自己常用的一些shell脚本分享
  • bash脚本中if语句的使用方法
  • Linux 下sudo网络权限详解
  • 浅析Linux系统下安装wetty和使用说明
  • Shell时间(date)相关命令
  • Linux中获取某个进程的系统调用以及参数(故障排查案例)
  • Linux SHELL if命令参数说明

文章分类

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

最近更新的内容

    • Linux makefile 和shell文件相互调用实例详解
    • 实现MySQL定时批量检查表repair和优化表optimize table的shell脚本
    • Linux中文件权限目录权限的意义及权限对文件目录的意义
    • 一天一个shell命令 linux好管家--磁盘--df命令详解
    • shift妙用之解决shell编程中的入参问题
    • 浅谈安装ORACLE时在Linux上设置内核参数的含义
    • 详解 linux mysqldump 导出数据库、数据、表结构
    • linux Shell脚本里面把一个数组传递到awk内部进行处理
    • Shell脚本实现上传zip压缩文件到FTP服务器
    • shell脚本实现拷贝大文件显示百分比的代码分享

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

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