1、介绍

expect是自动应答命令,对交互式命令自动化的执行。可用于自动化ssh及ftp。

2、expect中命令

spawn:监控程序,用于监控提出的交互性问题

send:发送字符串至交互问题

interact:回答完交互问题留在交互界面

exp_continue:匹配问题不存在时继续执行动作

expect eof:回答完交互问题退出expect

set NAME [ lindex $argv 0 ]:定义变量,0表示在expect脚本后第一个字符串,NAME表示变量名

3、测试

实现自动ssh自动登陆主机

(1)安装expect

命令:yum install expect.x86_64

[Linux]expect介绍

(2)编写登陆脚本

[Linux]expect介绍

并添加执行权限

命令:chmod +x auto_ssh.sh

(3)测试脚本正确性

[Linux]expect介绍

(4)编写expect脚本

文件后缀名称为.exp

[Linux]expect介绍

注:

在定义变量时,[ ]与lindex和数字之间需有空格

用于检测的脚本需添加执行权限

在expect 环境的{ }需要于expect之间存在空格

多个命令执行,如send和exp_continue需使用;进行分隔

\r 表示回车

可设定无法回答问题跳过问题的等待时间,命令:set timeout 3         为等待3秒回答下个问题

(5)测试自动ssh登陆

[Linux]expect介绍

 

相关文章:

  • 2021-07-26
  • 2022-12-23
  • 2021-05-19
  • 2021-12-10
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-09-27
  • 2022-02-20
  • 2021-08-18
  • 2022-01-06
  • 2022-12-23
相关资源
相似解决方案