Shell Programming Expect free interaction

Shell Programming Expect free interaction

Expect Overview

Expect installation

Expect basic commands

Expect implementation

Expect Case

Expect Overview

Expect

  • ExpectIt is built tclon the basis of a tool, Expecta tool used for automated control and testing. Mainly to solve the shellproblem of non-interactive script. For large-scale linuxoperation and maintenance helpful
  • In the linuxoperation and maintenance and development, we often need to operate a remote login server, the login process is an interactive process, you may need to enter yes/no passwordother information. To simulate this input, you can use the Expectscript

Expect installation

Mount CD

To make a local YUM source

The installation command

  • yum install expect -y
[root@localhost ~]# yum install expect -y
已加载插件:fastestmirror, langpacks
base                                                            | 3.6 kB  00:00:00     
extras                                                          | 2.9 kB  00:00:00     
updates                                                         | 2.9 kB  00:00:00     
(1/4): extras/7/x86_64/primary_db                               | 152 kB  00:00:00     
(2/4): base/7/x86_64/group_gz                                   | 165 kB  00:00:00     
(3/4): updates/7/x86_64/primary_db                              | 1.9 MB  00:00:01     
...//省略部分内容...
  正在安装    : 1:tcl-8.5.13-8.el7.x86_64                                          1/2 
  正在安装    : expect-5.45-14.el7_1.x86_64                                        2/2 
  验证中      : 1:tcl-8.5.13-8.el7.x86_64                                          1/2 
  验证中      : expect-5.45-14.el7_1.x86_64                                        2/2 

已安装:
  expect.x86_64 0:5.45-14.el7_1                                                        

作为依赖被安装:
  tcl.x86_64 1:8.5.13-8.el7                                                            

完毕!

Expect basic commands

send: send the string to the process used to simulate the user's input

  • This command does not automatically carriage return line, typically to add \r(carriage return)

expect

  • expectAn internal command, output in the last judgment contains a specified string, if there is an immediate return, otherwise wait timeout return.
  • Can only be captured by the spawnoutput of the process started

spawn: start the process, and subsequent interaction tracking information

interact: to maintain the cross-state execution is completed, the control to the console

Timeout: Specifies the timeout expired proceed to follow instructions

  • The unit is: seconds
  • timeout -1To never timeout
  • By default, timeouta 10second

exp_ continue

  • Allowed to expectcontinue executing instructions down

send_ user

  • Echo command, equivalent toecho

$ Argv array of parameters

  • ExpectScripts can be accepted from the bashparameters passed can be used [lindex $argv n]to obtain, nfrom 0the beginning, represent the first, second, third argument ...

Expect script must interact or expect eof end, perform automated tasks typically expect eof enough

  • expect eofWe are waiting for the end of the flag. By the spawncommand to start will produce at the end of a eofmark, expect eofRoyal waiting for this tag

Expect grammar

Single branch grammar

expect "password:" {send "mypassword\r";}

Multi-drop mode syntax

expect "aaa" {send"AAA\r"}   //send命令不具备回车换行功能,一般要加\r或\n
expect "aaa" {send"AAA\r"}
expect "aaa" {send"AAA\r"}
expect {
    "aaa" {send "AAA\r"}    //只要配置aaa或bbb或ccc中的任何一个,执行相应的send语句后退出该expect语句
    "bbb" {send "BBB\r"}
    "ccc" {send "CCC\r"}
}
expect {
    "aaa" {send "AAA";exp_continue}       //exp_continue表示继续后面的匹配,如果匹配了aaa,执行
    "bbb" {send "BBB";exp_continue}         完send语句后还要继续向下匹配bbb
    "ccc" {send "CCC"}
}

Expect implementation

Direct execution

  • Use expectfree interactive use sshTelnet to another server, and here I turn on two Linuxservers.

[root@localhost ~]# vim demo19.sh     //编辑脚本文件
#!/usr/bin/expect                     //expect二进制文件路径
set timeout 5                         //超时时间
log_file tast.log                     //记录日志文件存放位置
log_user 1                            //一个用户
set hostname [lindex $argv 0]         //设定第一个位置参数传递并设置变量名hostname
set password [lindex $argv 1]         //设定第二个位置参数传递并设置变量名password
spawn ssh root@$hostname              //追踪命令
expect {                              //使用expect命令捕捉条件,设定输入信息,进行免交互设置
        "(yes/no)"                    //设定匹配条件
        {send "yes\r";exp_continue}   //条件匹配并输入内容,并继续向下匹配
        "*password"                   //设定匹配条件
        {send "$password\r"}          //条件匹配,引用位置变量输入信息
}
interact                               //完成后将控制权交给控制台
[root@localhost ~]# chmod +x demo19.sh  //给脚本文件添加执行权限
[root@localhost ~]# ./demo19.sh 192.168.144.135 123123    //执行脚本
spawn ssh [email protected]
The authenticity of host '192.168.144.135 (192.168.144.135)' can't be established.
ECDSA key fingerprint is SHA256:B8IsZOFG7FbtVkIK+dMILmo0iA4OEIeVGY0GnnCbXhk.
ECDSA key fingerprint is MD5:c2:d8:09:17:de:6e:ec:07:06:1b:ac:b6:1e:bd:62:09.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.144.135' (ECDSA) to the list of known hosts.
[email protected]'s password: 
Last login: Sun Oct 13 23:29:38 2019 from 192.168.144.1     //自动登录服务器
[root@localhost ~]# ifconfig                           //查看服务器IP地址
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.144.135  netmask 255.255.255.0  broadcast 192.168.144.255    //成功登录
        inet6 fe80::a85a:c203:e2e:3f3c  prefixlen 64  scopeid 0x20<link>
        inet6 fe80::ad78:663f:1f02:22e4  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:75:9f:c8  txqueuelen 1000  (Ethernet)
        ...//省略部分内容...

Embed execution

[root@localhost ~]# vim demo20.sh     //编辑脚本文件
#!/bin/bash                           
hostname=$1                            //设置位置参数变量
password=$2
/usr/bin/expect <<-EOF          //加载expect功能,EOF前面加“-”是为增加容错性,-EOF 前面的“-"只能容错制表
spawn ssh root@$hostname          符,不能容错空格
expect {
        "(yes/no)"
        {send "yes\r";exp_continue}       //进行免交互配置
        "*password"
        {send "$password\r"}
}
expect "*]#"                              //捕捉内容结尾为“]#”的内容
send "exit\r"                             //匹配后输入exit退出登录
expect eof                                //等待结束标志
EOF                                       //结束标志
[root@localhost ~]# chmod +x demo20.sh    //添加执行权限
[root@localhost ~]# ./demo20.sh 192.168.144.135 123123    //执行脚本,并输入位置变量
spawn ssh [email protected]
[email protected]'s password: 
Last login: Sun Oct 13 23:58:05 2019 from 192.168.144.1
[root@localhost ~]# exit
登出
Connection to 192.168.144.135 closed.
[root@localhost ~]# ifconfig                       //查看IP地址信息
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.144.133  netmask 255.255.255.0  broadcast 192.168.144.255
        inet6 fe80::a85a:c203:e2e:3f3c  prefixlen 64  scopeid 0x20<link>   //成功登出
        ether 00:0c:29:5b:d3:a0  txqueuelen 1000  (Ethernet)
        RX packets 11058  bytes 855440 (835.3 KiB)
        ...//省略部分内容...

Guess you like

Origin blog.51cto.com/14473285/2442145