shell 编程-Expect

shell 编程-Expect

No.1 expect的安装

[root@newrain ~]# yum -y install expect

No.2 expect的语法

是一个免费的编程工具, 用来实现自动的交互式任务, 而无需人为干预. 说白了 expect 就是一套用来实现自动交互功能的软件

在实际工作中我们运行命令、脚本或程序时, 这些命令、脚本或程序都需要从终端输入某些继续运行的指令,而这些输 入都需要人为的手工进行. 而利用 expect 则可以根据程序的提示, 模拟标准输入提供给程序, 从而实现自动化交互执 行. 这就是 expect

能够在工作中熟练的使用Shell脚本就可以很大程度的提高工作效率, 如果再搭配上expect,那么很多工作都可以自动化 进行,对工作的展开如虎添翼

用法: 
1)定义脚本执行的shell
		#!/usr/bin/expect 类似于#!/bin/bash 
2)set timeout 30
		设置超时时间30s 
3)spawn
		spawn是进入expect环境后才能执行的内部命令,不能直接在默认的shell环境中执行 
		功能:传递交互命令
4)expect 
		这里的expect同样是expect命令
		功能:判断输出结果是否包含某项字符串,没有则立即返回,否则等待一段时间后返回,等待通过timeout设置 
5)send
		执行交互动作,将交互要执行的动作进行输入给交互指令 
		命令字符串结尾要加上“r”,如果出现异常等待状态可以进行核查
6)interract 
		执行完后保持交互状态,把控制权交给控制台 
		如果不加这一项,交互完成会自动退出
7)exp_continue 
		继续执行接下来的操作
expect环境中设置变量用set,识别不了bash方式定义的变量 
错误方式:
[root@newrain expect]# cat expect02.sh 
#!/usr/bin/expect
user=22
spawn echo $user
[root@newrain expect]# ./expect02.sh 
invalid command name "user=22"
    while executing
"user=22"
		(file "./expect02.sh" line 3)
正确方式:
[root@newrain expect]# cat ./expect02.sh 
#!/usr/bin/expect
set user 22
spawn echo $user
[root@newrain expect]# ./expect02.sh
spawn echo 22 ============================================================================

[root@newrain expect]# cat expect01.sh 
#!/usr/bin/expect
spawn ssh root@192.168.62.146
expect {
        "yes/no" { send "yes\r";exp_continue }
        "password:" { send "123\r" }
} 
interact

interact的作用测试
执行测试是否免交互:
要是用/usr/bin/expect的shell执行
[root@newrain expect]#/usr/bin/expect expect01.sh 
成功
擦除最后一行interact进行测试
[root@newrain expect]#/usr/bin/expect expect01.sh
进入之后立即退出 ============================================================================
\r的作用测试
[root@newrain expect]# cat expect01.sh 
#!/usr/bin/expect
set user root
set pass 123
set ip 192.168.62.146
spawn ssh $user@$ip
expect {
        "yes/no" { send "ye";exp_continue }
        "password:" { send "pas" }
} 
interact
[root@newrain expect]# ./expect01.sh 
spawn ssh root@192.168.62.146 
root@192.168.62.146's password:

加上\r之后
[root@newrain expect]# ./expect01.sh
spawn ssh root@192.168.62.146
root@192.168.62.146's password:
Permission denied, please try again.
root@192.168.62.146's password: ============================================================================
设置变量的方式
[root@newrain expect]# cat expect01.sh #!/usr/bin/expect
set user root
set pass 123
set ip 192.168.62.146 
spawn ssh $user@$ip 
expect {
        "yes/no" { send "yes\r";exp_continue }
        "password:" { send "$pass\r" }
}
interact
============================================================================
设置位置参数的方式(拓展)
[root@newrain expect]# cat expect01.sh 
#!/usr/bin/expect
set timeout 30
set user [ lindex $argv 0 ]
set pass [ lindex $argv 1 ]
set ip [ lindex $argv 2 ]
spawn ssh $user@$ip

expect {
        "yes/no" { send "yes";exp_continue }
        "password:" { send "$pass\r" }
}
interact
运行结果为:
[root@newrain expect]# ./expect01.sh root 123 192.168.62.146 
spawn ssh root@192.168.62.146
root@192.168.62.146's password:
Last login: Thu Aug 15 23:26:59 2019 from 192.168.62.136
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值