go-Expect-实验

实验脚本程序

test.sh

read -p "Would you like to rejoin it to the cluster? [y/N]:" v
echo
if [ "$v" == "y" ];then
    echo "$(date +%s)shark A" >> ./test.log
fi
read -p "Would you like to rejoin it to the cluster? [y/N]:" v
if [ "$v" == "y" ];then
    echo "$(date +%s)shark B" >> ./test.log
fi

read -p "Would you like to remove it from the cluster's metadata? [y/N]:" v2
if [ "$v2" == "n" ];then
    echo "shark C"
fi
echo "The cluster was successfully rebooted."

go 程序

main.go

package main

import (
    "fmt"
    "regexp"
    "time"
    "github.com/google/goexpect"
 //   "google.golang.org/grpc/codes"
)

const (
    timeout = 10 * time.Second
)

func main(){
    cmd := "sh test.sh"
    member :=  [1]string{"mysql1"}

    for _, _ = range member{
        e, _, err := expect.Spawn(cmd, -1)
        defer e.Close()

        ot, err := e.ExpectBatch([]expect.Batcher{
            &expect.BCas{[]expect.Caser{
            &expect.Case{R: regexp.MustCompile(`rejoin it to the cluster\? \[y/N]:`), S: "y\n", T:  expect.Next(), Rt: 1},
            &expect.Case{R: regexp.MustCompile(`rejoin it to the cluster\? \[y/N]:`), S: "y\n", T:  expect.Next(), Rt: 1},
            &expect.Case{R: regexp.MustCompile(`remove it from the cluster's metadata\? \[y/N]:`), S: "n\n", T: expect.Next(), Rt: 1},
            &expect.Case{R: regexp.MustCompile(`The cluster was successfully rebooted`), T: expect.OK()},
                }},
        }, timeout)
        fmt.Println("ot:", ot, "err:", err)
    }
}
            //&expect.Case{R: regexp.MustCompile(`rejoin it to the cluster\? \[y/N]:`), S: "y\n", T:  expect.Continue(expect.NewStatus(codes.OK, "继续")), Rt: 1},

相关推荐

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-05-11 17:02:06       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-11 17:02:06       106 阅读
  3. 在Django里面运行非项目文件

    2024-05-11 17:02:06       87 阅读
  4. Python语言-面向对象

    2024-05-11 17:02:06       96 阅读

热门阅读

  1. Linux 第二十六章

    2024-05-11 17:02:06       33 阅读
  2. vue3-seamless-scroll实现循环滚动

    2024-05-11 17:02:06       27 阅读
  3. 以太网网络变压器型号

    2024-05-11 17:02:06       29 阅读
  4. git 更换远程仓库地址三种方法总结

    2024-05-11 17:02:06       28 阅读
  5. 【笔记】Android MVNO APN 字段配置方法

    2024-05-11 17:02:06       27 阅读
  6. react18【系列实用教程】useState (2024最新版)

    2024-05-11 17:02:06       38 阅读
  7. impdp恢复表后发现比原表多了100多行

    2024-05-11 17:02:06       32 阅读
  8. C++ 多线程笔记1 线程的创建

    2024-05-11 17:02:06       30 阅读
  9. 【Python快速上手(十九)】

    2024-05-11 17:02:06       30 阅读
  10. 【go从入门到精通】精通并发编程-协程goroutine

    2024-05-11 17:02:06       28 阅读
  11. 【算法入门教育赛2】C.曼哈顿种类 C++题解与代码

    2024-05-11 17:02:06       35 阅读
  12. 安卓提示Cannot resolve symbol ‘BuildConfig‘

    2024-05-11 17:02:06       31 阅读