ZhiBing's blog(码上看世界) ZhiBing's blog(码上看世界)
首页
  • Linux工具链

    • shell命令
  • 构建

    • CMake
    • Makefile
  • 版本管理

    • Git
    • Github
  • IDE及工具

    • vscode
    • CLion
  • 设计模式

    • 设计原则
  • 编程语言

    • C++
    • Go
    • Python
    • Shell
  • 调试

    • gdb
  • 开发者测试

    • gtest
  • 系统支撑

    • 操作系统
  • 性能优化

    • 编译优化选项
    • perf
    • valgrind
  • 容器

    • Docker
  • 微服务

    • Rancher
  • 其他
  • 随笔
  • 友情链接
收藏
  • 分类
  • 标签
  • 归档
关于
GitHub (opens new window)

ZhiBing Zheng

时间会回答成长
首页
  • Linux工具链

    • shell命令
  • 构建

    • CMake
    • Makefile
  • 版本管理

    • Git
    • Github
  • IDE及工具

    • vscode
    • CLion
  • 设计模式

    • 设计原则
  • 编程语言

    • C++
    • Go
    • Python
    • Shell
  • 调试

    • gdb
  • 开发者测试

    • gtest
  • 系统支撑

    • 操作系统
  • 性能优化

    • 编译优化选项
    • perf
    • valgrind
  • 容器

    • Docker
  • 微服务

    • Rancher
  • 其他
  • 随笔
  • 友情链接
收藏
  • 分类
  • 标签
  • 归档
关于
GitHub (opens new window)
  • 编程语言

    • C++

    • Go

    • Python

    • shell

      • shell脚本中数组作为参数传递
      • shell批量替换文件名
      • shell统计文件数量
      • shell脚本获取进程pid
      • shell脚本判断文件后缀
      • shell脚本中调用expect
    • Rust

  • 调试

  • 开发者测试

  • 系统支撑

  • 性能优化

  • 通用领域
  • 编程语言
  • shell
zhengzhibing
2022-06-16

shell脚本中调用expect

# shell 脚本中调用 expect

expect 是 unix/linux 系统中用来进行自动化控制和测试的软件工具。该工具利用伪终端包装其子进程,允许任意程序通过终端接入进行自动化控制。

首先安装expect,sudo apt-get install expect。

介绍expect命令之前,先介绍几个和expect有关的命令:

  • spawn: spawn 命令可以启动一个脚本或程序,类似 shell,ftp,telnet,ssh,scp 等等;
  • expect: 等待程序输出,并匹配这些输出的内容
  • send: 发送一个回应给程序
  • exp_continue: 继续匹配
  • set: 定义变量
  • puts: 输出变量
  • exit: 退出 expect 命令
  • interact: 运行你和程序进行交互
# 设置命令执行的超时时间(s)
set timeout 10
# 使用spawn命令启动一个ssh命令
spawn ssh user@127.0.0.1

# 捕获程序输出的`paasword`字符串,如果捕获到了,则发送`paaswd\n`字符串到终端
expect "password" { send "paaswd\n" }
# 退出expect命令
expect eof
1
2
3
4
5
6
7
8
9

ssh 自动登录实例:

# --------------------------------------------------------------------------- #
# ssh自动登录
# Parameter1: 用户名
# Parameter2: ip地址
# Parameter3: 密码
# output: None
# return: None
# --------------------------------------------------------------------------- #
function autossh() {
    local user=$1
    local ip=$2
    local passwd=$3
    /usr/bin/expect <<-EOF
    set timeout 10
    spawn ssh $user@$ip
    expect {
        "yes/no" { send "yes\n"; exp_continue }
        "password" { send "$passwd\n" }
    }
    interact
EOF
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

ssh 自动登录并切换到 root 用户:

user="user"
ip="xxx.xxx.xxx.xxx"
passwd="xxx"
root_pwd="xx"

/usr/bin/expect <<-EOF
set timeout 10
spawn ssh $user@$ip
expect {
    "yes/no" { send "yes\n"; exp_continue }
    "password" { send "$passwd\n" }
}
expect "login" { send "su - root\n" }
expect "Password" { send "$root_pwd\n" }
expect "~#" { send "echo hello\n" }
expect eof
EOF
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#shell
上次更新: 2022/06/17, 07:22:19
shell脚本判断文件后缀
01安装rust

← shell脚本判断文件后缀 01安装rust→

最近更新
01
HPE gen10 plus 安装ESXI 7
06-12
02
ESXI 7安装黑群晖
06-12
03
ESXI 7安装win10
06-12
更多文章>
Theme by Vdoing | Copyright © 2022-2024 ZhBing Zheng | 粤ICP备2022062743号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式