eof怎么结束输入_详解自动交互命令expect,免去手动输入!

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

#扩展TCL:全拼为Tool Command Language ,是一种脚本语言,由John Ousterout创建。TCL功能很强大,经常被用于快速原型开发,脚本编程,GUI和测试等方面

#使用背景
在执行系统命令或程序时,有些系统会以交互式的形式要求输出指定的字符串之后才能执行命令,如用户设置密码,一般都是需要手工输入2次密码,再如SSH登录的,如果没有做免密钥登录,第一次连接要和系统实现两次交互式输入

#安装
yum install expect

#自动交互工作流程
spawn启动指定进程--->expect获取期待的关键字-->send向指定进程发送指定字符-->进程执行完毕,退出结束

相关使用命令

#1.spawn命令
在expect自动交互程序执行的过程中,spawn命令是一开始就需要使用的命令。通过spawn执行一个命令或程序,之后所有的expect操作都会在这个执行过的命令或程序进程中进行,包括自动交互功能,因此如果没有spawn命令,expect程序将会无法实现自动交互
#语法
spawn [选项] [需要自动交互的命令或程序]

#示例
spawn ssh root@192.168.1.1 uptime
#在spawn命令的后面,直接加上要执行的命令或程序(例如这里的ssh命令)等,除此之外,spawn还支持如下一些选项
-open: 表示启动文件进程
-ignore:表示忽略某些信号
#提示:使用spawn命令expect程序实现自动 交互工作流程的第一步,也是最关键的一步

#2.expect命令
expect命令的作用就是获取spawn命令执行后的信息,看看是否和其事先指定的相匹配。一旦匹配上指定的内容就执行expect后面的动作,expect命令也有一些选项,相对用的较多的是-re,使用正则表达式的方式来匹配
#语法
expect 表达式 [动作]

#示例
spawn ssh root@192.168.1.1 uptime
expect "*password" {send *123456r"}
#提示:上述命令不能直接在linux命令行中执行,需要放入expect脚本中执行

#示例
ssh远程登录机器执行命令,首次登录需要输入yes/no和输入密码

[root@game scripts]# cat ssh.exp 
#!/usr/bin/expect
​
spawn ssh root@192.168.228.137 "free -m"
expect {
    "yes/no" {exp_send "yesr";exp_continue}
    "*password" {exp_send "guoke123r"}
}
expect eof
#参数说明
exp_send和send类似。r(回车)
匹配多个字符串的时候,需要在每次匹配并执行动作后,加上exp_continue

#3.send命令
即在expect命令匹配指定的字符串后,发送指定的字符串给系统,这些命令可以支持一些特殊转义符号,例如:r表示回车、n表示换行、t表示制表符等

#参数
-i: 指定spawn_id,用来向不同的spawn_id进程发送命令,是进行多程序控制的参数
-s: s代表slowly,即控制发送的速度,使用的时候要与expect中的标量send slow相关联

#4.exp_continue命令
作用是让expect程序继续匹配的意思 #如

expect {
    "yes/no" {exp_send "yesr";exp_continue}
    "*password" {exp_send "guoke123r"}
}

#因为后面还有匹配的字符,所以需要加上exp_continue,否则expect将不会自动输入指定的字符串,最后一个就不需要加上exp_continue了

#5.send_user命令
send_user命令可用来打印expect脚本信息,类似shell里的echo命令
#用法

[root@game scripts]# cat send_user.exp 
#!/usr/bin/expect
​
send_user "guoken"
send_user "youtiao.t" #tab键,没有换行
send_user "what haon"
​
#效果
[root@game scripts]# expect send_user.exp 
guoke
youtiao.    what hao

#6.exit命令
exit命令的功能类似于shell中的exit,即直接退出expect脚本,除了最基本的退出脚本功能之外,还可以利用这个命令对脚本做一些关闭前的清理和提示等工作

expect程序变量

#1.普通变量
expect中的变量定义,使用方法与TCL语言中的变量基本相同

#语法
set 变量名 变量值

#示例
set user "guoke"

#打印变量语法
puts $变量名

#例子

[root@game scripts]# cat test1.exp 
#!/usr/bin/expect
set password "guoke123"
puts $password
​
#效果
[root@game scripts]# expect test1.exp 
guoke123

#2.特殊变量
在expect里也有与shell脚本里的$0、$!、$#等类似的特殊参数变量,用于接收及控制expect脚本传参

在expect中$argv表示参数数组,可以使用[lindex $argv n]接收expect脚本传参,n从0开始,分别表示第一个[lindex $argv 0]参数、第二个[lindex $argv 1]参数、第三个[lindex $argv 2]参数.....

[root@game scripts]# cat test2.exp 
#!/usr/bin/expect

set file [lindex $argv 0]
set id [lindex $argv 1]
set host [lindex $argv 2]
puts "$filet$idt$hostn"
send_user "$filet$idt$hostn"

#效果
[root@game scripts]# expect test2.exp test.log 1 192.168.1.1
test.log    1   192.168.1.1
test.log    1   192.168.1.1

#扩展
除了基本的位置参数外,expect也支持其他的特殊参数,例如:$argc表示传参的个数,$argv0表示脚本的名字。

#示例

[root@game scripts]# cat test2.exp 
#!/usr/bin/expect

set file [lindex $argv 0]
set id [lindex $argv 1]
set host [lindex $argv 2]
puts "$filet$idt$hostn"
puts $argc
puts $argv0

#效果
[root@game scripts]# expect test2.exp test.log 1 192.168.1.1
test.log 1 192.168.1.1
3 #传参的总数
test2.exp #脚本的名字

expect中的if条件语句

#语法

if {条件表达式} {
    指令
}

或
if {条件表达式} {
    指令
} else {
    指令
}

#提示:if关键字后面要有空格,else关键字前后都要有空格,{条件表达式}大括号里面靠近大括号出可以没有空格,将指令括起来的起始大括号”{“ 前要有空格

#示例1
#使用if语句判断脚本传参的个数,如果不符合则给予提示

[root@game scripts]# cat test3.exp 
#!/usr/bin/expect
​
if { $argc != 3 } {
    send_user "usage:expect $argv0 file id hostn"
    exit
}
​
set file [lindex $argv 0]
set id [lindex $srgv 1]
set host [lindex $argv 2]
puts "$filet$idt$hostn"
​
#效果
[root@game scripts]# expect test3.exp 
usage:expect test3.exp file id host

#示例2 #使用if语句判断脚本传参的个数,不管是否符合都给予提示

[root@game scripts]# cat test4.exp 
#!/usr/bin/expect
if {$argc != 10} {
    send_user "bey.n"
} else {
    send_user "okn"
}

#效果
[root@game scripts]# expect test4.exp 
bey.

expect中的关键字

#介绍
expect中的特特殊关键字用于匹配过程,代表某些特殊的含义或状态,一般只用于expect命令中而不能在expect命令单独使用

#1.eof关键字
eof(end-of-file文件结尾)关键字用于匹配结束符
#示例

[root@game scripts]# cat ssh.exp 
#!/usr/bin/expect
​
spawn ssh root@192.168.228.137 "free -m"
expect {
    "yes/no" {exp_send "yesr";exp_continue}
    "*password" {exp_send "guoke123r"}
}
expect eof

#2.timeout关键字
timeout是expect中的一个控制时间的关键字变量,它是一个全局性的时间控制开关,可以通过为这个变量赋值来规定整个expect操作的时间,注意这个变量是服务于expect全局的,而不是某一条命令,即使命令没有任何错误,到了时间仍然会激活这个变量,此外,到时间后还会激活一个处理及提示信息开关,
#示例

[root@game scripts]# cat ssh.exp 
#!/usr/bin/expect

spawn ssh root@192.168.228.137 "free -m"
expect {
 -timeout 3
    "yes/no" {exp_send "yesr";exp_continue}
    "*password" {exp_send "guoke123r"}
 timeout {puts "request timeout;return"}
}
#效果
[root@game scripts]# expect ssh.exp 
spawn ssh root@192.168.228.137 free -m
request timeout #当时间超过3秒就会打印超时退出

#应用例子
1.复制文件

[root@game scripts]# cat scp.exp 
#!/usr/bin/expect

set src_file [lindex $argv 0]
set dst_file [lindex $argv 1]
set host [lindex $argv 2]
set username [lindex $arv 3]
set password [lindex $arv 4]

spawn scp $src_file $username@$host:$dst_file
expect {
    -timeout 15
    "yes/no" {exp_send "yesr";exp_continue}
    "*password" {exp_send "guoke123r"}
    timeout {puts "request timeout";return}
}
expect eof

#效果
[root@game scripts]# expect scp.exp test.txt test.txt 192.168.228.137 root guoke123
spawn scp test.txt root@192.168.228.137:test.txt
root@192.168.228.137's password: 
test.txt                             100%    3     3.4KB/s   00:00   

#到目标主机上查看
[root@zabbix ~]# ls -l test.txt 
-rw-r--r-- 1 root root 3 Aug 20 05:32 test.txt
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值