expect工具

9.1 expect工具

9.1.1 定义

写交互式脚本工具,也是一门语言

9.1.2 安装

[root@localhost ~]#yum install expect 

9.1.3 定义变量

1.set 变量名 值

2.set 变量名 [lindex $argv 0]

3.特殊变量

  • $argc 参数个数
  • $argv 所有参数数组,索引号从0开始

9.1.4 语句

1.set 定义变量

  • set timeout 300 #单位秒

2.spawn COMMAND

  • 监控命令的输出

3.expect “STRING” { send “STRING” } 捕获输出,并执行其他命令

4.send “STRING\r” 发送"String"

5.send_user “STRING” 输出信息,相当于shell中的echo

扫描二维码关注公众号,回复: 9714145 查看本文章

6.exp_continue 继续捕获输出

7.interact 停留在交互界面,将控制权交给用户

5.expect脚本创建方式

1.创建.exp结尾的文件

第一行#!/usr/bin/expect

9.1.5 脚本

#!/usr/bin/expect

set user "root"
set pass "xxx"
set ip "192.168.217.130"


spawn ssh $user@$ip
expect {
        "yes/no" {
                send "yes\r"
                exp_continue
        }
        "password: " {
                send "$pass\r"
                exp_continue
        }

}

expect "#" {
		send "echo 123\r"
        send "exit\r"
}
 

expect eof
send_user "123\r"

在这里插入图片描述

要注意加换行符或者回车。。。

发布了134 篇原创文章 · 获赞 16 · 访问量 6320

猜你喜欢

转载自blog.csdn.net/weixin_46108954/article/details/104707045
今日推荐