实验脚本程序
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"
"/google/goexpect"
// "/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},