zoukankan      html  css  js  c++  java
  • 使用 expect 命令执行自动分发系统

    一、命令 except 实例详解

    1. 介绍 expect 使用场景

    expect可以让我们实现自动登录远程机器,并且可以实现自动远程执行命令。当然若是使用不带密码的密钥验证同样可以实现自动登录和自动远程执行命令。但当不能使用密钥验证的时候,我们就没有办法了。所以,这时候只要知道对方机器的账号和密码就可以通过expect脚本实现登录和远程命令。

    使用之前先安装 expect 软件

    yum install -y expect

    2. 自动远程登录,登陆后不退出

    #! /usr/bin/expect
    set user "root"
    set host "192.168.11.102"
    set passwd "123456"
    spawn ssh  $user@$host
    expect {
    "yes/no" { send "yes
    "; exp_continue}
    "assword:" { send "$passwd
    " }
    }
    interact

    3. 登录后执行命令,然后退出

    #!/usr/bin/expect
    set user "root"
    set host "192.168.11.18"
    set passwd "123456"
    spawn ssh $user@$host
    expect {
    "yes/no" { send "yes
    "; exp_continue}
    "password:" { send "$passwd
    " }
    }
    expect "]*"
    send "touch /tmp/12.txt
    "
    expect "]*"
    send "echo 1212 > /tmp/12.txt
    "
    expect "]*"
    send "exit
    "

    4. 还可以传递参数

    #!/usr/bin/expect
    set user [lindex $argv 0]
    set host [lindex $argv 1]
    set passwd "123456"
    set cm [lindex $argv 2]
    spawn ssh $user@$host
    expect {
    "yes/no" { send "yes
    "}
    "password:" { send "$passwd
    " }
    }
    expect "]*"
    send "$cm
    "
    expect "]*"
    send "exit
    "

    执行:

    [root@localhost ~]# ./test.expect root 192.168.11.18 2

    5. 自动同步文件

    #!/usr/bin/expect
    set passwd "123456"
    spawn rsync -av root@192.168.11.18:/tmp/12.txt /tmp/
    expect {
    "yes/no" { send "yes
    "}
    "password:" { send "$passwd
    " }
    }
    expect eof

    6. 指定 host 和要同步的文件

    #!/usr/bin/expect
    set passwd "123456"
    set host [lindex $argv 0]
    set file [lindex $argv 1]
    spawn rsync -av $file root@$host:$file
    expect {
    "yes/no" { send "yes
    "}
    "password:" { send "$passwd
    " }
    }
    expect eof

    执行:

    [root@localhost ~]# ./test.expect 192.168.11.18 /tmp/12.txt

     

    二、构建文件分发系统

    1. 需求背景

    对于大公司而言,肯定时不时会有网站或者配置文件更新,而且使用的机器肯定也是好多台,少则几台,多则几十甚至上百台。所以,自动同步文件是至关重要的。

    2. 实现思路

    首先要有一台模板机器,把要分发的文件准备好,然后只要使用expect脚本批量把需要同步的文件分发到目标机器即可。

    3. 核心命令

    rsync -av --files-from=list.txt  /  root@host:/

    4. 文件分发系统的实现

     rsync.expect

    #!/usr/bin/expect
    set passwd "123456"
    set host [lindex $argv 0]
    set file [lindex $argv 1]
    spawn rsync -av --files-from=$file / root@$host:/
    expect {
    "yes/no" { send "yes
    "}
    "password:" { send "$passwd
    " }
    }
    expect eof

    rsync.sh

    #!/bin/bash
    for ip in `cat ip.list`
    do
        echo $ip
        ./rsync.expect $ip list.txt
    done

    5. 自动批量执行脚本命令

    cat exe.expect

    #!/usr/bin/expect
    set host [lindex $argv 0]
    set passwd "123456"
    set cm [lindex $argv 1]
    spawn ssh root@$host
    expect {
    "yes/no" { send "yes
    "}
    "password:" { send "$passwd
    " }
    }
    expect "]*"
    send "$cm
    "
    expect "]*"
    send "exit
    "

    cat exe.sh

    #!/bin/bash
    for ip in `cat ip.list`
    do
        echo $ip
        ./exe.expect $ip "w;free -m;ls /tmp"
    done

    三、扩展 expect 语法介绍

    shell脚本需要交互的地方可以使用 Here 文档是实现,但是有些命令却需要用户手动去就交互如passwd、scp。对自动部署免去用户交互很痛苦,expect能很好的解决这类问题。

    1. expect的核心是spawn expect send set

    spawn:调用要执行的命令
    expect:等待命令提示信息的出现,也就是捕捉用户输入的提示:
    send:发送需要交互的值,替代了用户手动输入内容
    set:设置变量值
    interact:执行完成后保持交互状态,把控制权交给控制台,这个时候就可以手工操作了。如果没有这一句登录完成后会退出,而不是留在远程终端上。
    expect eof:这个一定要加,与 spawn 对应表示捕获终端输出信息终止,类似于 if....endif
    expect 脚本必须以 interact 或 expect eof 结束,执行自动化任务通常 expect eof 就够了。

    2. 设置 timeout

    设置 expect 永不超时
    set timeout -1

    设置 expect 300 秒超时,如果超过 300 没有 expect 内容出现,则推出
    set timeout 300

  • 相关阅读:
    史上最全Java表单验证封装类
    QQ组件可导致IE10无响应
    如何获取特定用户组内的无效账户?
    IN2Windows 8 (Part 2)
    IN2Windows 8 (Part 4) 文件历史记录功能及其重置方法
    IN2Windows 8 (Part 3)
    Android 多文件监听的实现
    Android 调用打电话,发短信(彩信),发邮件,浏览器,分享,跳转系统的各个设置页面
    Android中Drawable小结
    Android 加载.gif格式图片
  • 原文地址:https://www.cnblogs.com/liwei0526vip/p/6193424.html
Copyright © 2011-2022 走看看