Expect自动化交互程序

Expect介绍:

1、什么是Expect

Expect是一个用来实现自动化交互功能的软件套件,基于TCL的脚本编程工具语言,方便学习,功能强大。

2、为什么要使用expcet:

当今的企业运维中,自动化运维已经成为运维的主流趋势,但是在很多情况下,执行系统命令或程序时,系统会以交互的形式要求运维人员输入指定的字符串,之后才能继续执行命令,这个时候我们就需要expect交互工具来帮忙了,expect可以模拟手工交互过程,实现自动与远端程序的交互,从而达到自动化运维的目的。

Expect命令 z作用
spawn spawn命令是一个在Expect自动交互程序的开始就需要使用的命令,通过spawn执行一个命令或程序,之后所有的Expect操作都在这个执行过的命令或程序进程中执行
expect z在expect自动交互程序执行的过程中,在 使用spawn命令执行一个命令或程序之后,会提示某些交互信息,expect命令的作用就是获取这些信息,查看是否和其事先指定的信息相匹配,一旦匹配上指定的内容,就执行expect后面的内容
send 

Expect中的动作命令,当expect匹配了指定的字符串之后,发送指定的字符串给系统,这些命令可以支持一些特殊的转义符号,例如:\r \n \t

还有一个类似的命令exp_send命令

exp_continue z属于一个动作命令,在一个expect命令中,用于多次匹配字符串并执行不同的动作中
send_user

l类似shell的echo命令

exit t退出Expect脚本,以及退出前做一些关闭前的清理和提示等工作

示例脚本:

执行ssh命令远程获取服务器的负载值

#!/usr/bin/expect
spawn ssh root@192.168.8.31 uptime
expect {
    "yes/no"    {exp_send "yes\r";exp_continue}
    "*password" {exp_send "123456\r"}
}
expect eof 
#!/bin/bash
read -p "please input your username:" name
read -p "please input your password" pass
read -p "input your email" mail
echo -n "your name is $name,"
echo -n "your pass is $pass,"
echo  "your email is $mail."

#!/usr/bin/expect
spawn /bin/sh read.sh
expect {
    "username" {exp_send "leon\r";exp_continue}
    "*pass" {exp_send "123456\r";exp_continue}
    "*mail" {exp_send "[email protected]\r";}
   
    }   
expect eof 
View Code
#!/usr/bin/expect
set file [lindex $argv 0]     ##[lindex $argv n] 接收脚本传参 n从0开始 分别表示第一个参数[lindex $argv 0] 第二个参数 [lindex $argv 1].....
set host [lindex $argv 1]
set dir  [lindex $argv 2]
puts "$file\t$host\t$dir"    ##puts打印变量
puts $argc             ##$argc表示参数的总个数
puts $argv0            ##$argv0 表示脚本的名字

[root@localhost scripts]# expect 1.exp  1 2 3
1       2       3
3
1.exp

Expect中的if条件语句

if { 条件表达式 } {

  指令

}

或

if { 条件表达式 } {

  指令

} else { 指令

}
#!/usr/bin/expect
if { $argc != 3 } {                         #argc为传参的个数,相当于shell里面的$#
    send_user "Usage expect $argv0 file host dir\n"          
    exit 
    }   
    set file [lindex $argv 0]
    set host [lindex $argv 1]
    set dir  [lindex $argv 2]
    puts "$file\t$host\t$dir"                      #puts相当于echo

实现自动交互脚本

#!/usr/bin/expect
if { $argc != 2 } { 
    send_user "Usage expect $argv0 ip command"
    exit 
    }   
    set ip [lindex $argv 0]
    set cmd [lindex $argv 1]
    set password "123456"
    spawn ssh root@$ip $cmd
    expect {
        "yes/no" {send "yes\r";exp_continue}
        "*password" {send "$password\r"}
    }
    expect eof

实现自动交互式批量发送文件或目录:

test.exp

#!/usr/bin/expect
if { $argc != 3 } { 
    send_user "Usage expect $argv0 file host dir "
    exit 
    }   
    set file [lindex $argv 0]
    set host [lindex $argv 1]
    set dir  [lindex $argv 2]
    set password "123456"
    scp -P22 -rp $file  root@$host:$dir
    expect {
        "yes/no" {send "yes\r";exp_continue}
        "*password" {send "$password\r"}
    }
    expect eof

利用shell循环执行脚本

#!/bin/bash
if [ $# -ne 2 ];then
    echo "Usage:$0 file dir"
    exit 1
fi
file=$1
dir=$2
for n in 128 129 130 
    do  
        expect test.exp $file 192.168.33.$n $dir
    done

开发Expec提交本自动化分发公钥到所有服务器

#!/usr/bin/expect
if { $argc != 2 } {
    send_user "usage: expect expect.exp file host\n"
    exit
    }
set file [lindex $argv 0]
set host [lindex $argv 1]
set password "123456"

spawn ssh-copy-id -i $file "-p 22 root@$host"
expect {
    "yes/no" {send "yes\r";exp_continue}
    "*password" {send "$password\r"}
    }
    expect eof

开发shell脚本循环执行expect脚本分发公钥

#!/bin/sh
for n in 128 129 130
do
     expect test.exp ~/.ssh/id_dsa.pub $192.168.33.$n
done

猜你喜欢

转载自www.cnblogs.com/Template/p/9275680.html