如何筛选出可行的案件执行方案?
时间:2024-02-13
浏览:191
作者:佚名
最近发现不少小伙伴都对Golang很感兴趣,所以今天继续给大家介绍Golang相关的知识,本文《如何筛选出可行的案件执行方案?》主要内容涉及到等等知识点,希望能帮到你!当然如果阅读本文时存在不同想法,可以在评论中表达,但是请勿使用过激的措辞~
问题内容
我使用https://github.com/google/goexpect作为我的shell交互,pppoe拨号程序,我的操作系统centos7,我的拨号程序rp-pppoe,第一次使用,如1.
拨号时如何编写 goexpect 交互?
我知道问题出在哪里,但不知道如何解决。当我创建并发表盘时,第一次交互的逻辑与后续交互的逻辑不同。例如,第二篇文章的内容与第一篇文章的内容不同,它添加了一个新问题:(默认ppp0)或者如果你想创建一个新问题,请输入“n”:,我看到了goexpect 有 expectswitchcase 的操作,但我不太明白如何用它解决我的问题。如何匹配其中一种交互逻辑并忽略其他条件。
- 第一次执行交互
welcome to the pppoe client setup. first, i will run some checks on your system to make sure the pppoe client is installed properly... login name enter your login name (default root): user
- 当我执行第二次交互时,如何使用
google/goexpect匹配多个条件中的一个,并继续执行下一步
welcome to the pppoe client setup. first, i will run some checks on your system to make sure the pppoe client is installed properly... the following dsl config was found on your system: device: name: ppp0 dslppp0 please enter the device if you want to configure the present dsl config (default ppp0) or enter 'n' if you want to create a new one:n login name enter your login name: user
- 我的代码,但无法正常运行
package shell
import (
"fmt"
expect "github.com/google/goexpect"
"github.com/google/goterm/term"
"regexp"
"time"
)
func r(user, password, dev string) error {
fmt.println(term.bluef("telnet spawner example"))
to := 3 * time.second
exec, _, err := expect.spawn("/usr/sbin/pppoe-setup", to)
if err != nil {
return err
}
defer exec.close()
r, rs, _, err := exec.expectswitchcase([]expect.caser{
&expect.case{r: regexp.mustcompile(`enter your login name \(default root\):`), s: user + "\n", t: expect.next(), rt: 0},
&expect.case{r: regexp.mustcompile(`or enter 'n' if you want to create a new one:`), s: "n\n"},
}, to)
if err != nil {
return err
}
fmt.println("r:rs:", r, rs)
ur, urs, err := exec.expect(regexp.mustcompile(`enter your login name`), to)
if err != nil {
return err
}
fmt.println("ur:urs:", ur, urs)
if err := exec.send(user + "\n"); err != nil {
fmt.println("here03:", err.error())
return err
}
dr, drs, err := exec.expect(regexp.mustcompile(`\(default eth0\):`), to)
if err != nil {
fmt.println("here04:", err.error())
return err
}
fmt.println(dr, drs)
if err := exec.send(dev + "\n"); err != nil {
fmt.println("here05:", err.error())
return err
}
er, ers, err := exec.expect(regexp.mustcompile(`enter the demand value \(default no\):`), to)
if err != nil {
fmt.println("here06:", err.error())
return err
}
fmt.println(er, ers)
if err := exec.send("\n"); err != nil {
fmt.println("here06x:", err.error())
return err
}
if _, _, err := exec.expect(regexp.mustcompile(`enter the dns information here:`), to); err != nil {
fmt.println("here06x:", err.error())
return err
}
if err := exec.send("\n"); err != nil {
fmt.println("here07:", err.error())
return err
}
if _, _, err := exec.expect(regexp.mustcompile(`please enter your password:`), to); err != nil {
fmt.println("here08:", err.error())
return err
}
if err := exec.send(password + "\n"); err != nil {
fmt.println("sendhere08:", err.error())
return err
}
if _, _, err := exec.expect(regexp.mustcompile(`please re-enter your password:`), to); err != nil {
fmt.println("here0801:", err.error())
return err
}
if err := exec.send(password + "\n"); err != nil {
fmt.println("sendhere0801:", err.error())
return err
}
if _, _, err := exec.expect(regexp.mustcompile(`normal user to start or stop dsl connection \(default yes\):`), to); err != nil {
fmt.println("here09:", err.error())
return err
}
if err := exec.send("yes\n"); err != nil {
fmt.println("here10:", err.error())
return err
}
if _, _, err := exec.expect(regexp.mustcompile(`choose a type of firewall \(0-2\):`), to); err != nil {
fmt.println("here11:", err.error())
return err
}
if err := exec.send("0\n"); err != nil {
fmt.println("here12:", err.error())
return err
}
if _, _, err := exec.expect(regexp.mustcompile(`please enter no or yes \(default no\):`), to); err != nil {
fmt.println("here13:", err.error())
return err
}
if err := exec.send("yes\n"); err != nil {
fmt.println("here14:", err.error())
return err
}
fr, frs, err := exec.expect(regexp.mustcompile(`accept these settings and adjust configuration files \(y/n\)?`), to);
if err != nil {
fmt.println("here15:", err.error())
return err
}
fmt.println(fr,frs)
if err := exec.send("y\n"); err != nil {
fmt.println("here16:", err.error())
return err
}
return nil
}
- 这是主文件@josh chappelle
package main
import (
"fmt"
"github.com/seraphico/ppps/pkg/shell"
)
func main() {
err := shell.R("test01", "test01", "br3")
if err != nil {
fmt.Println(err.Error())
return
}
}正确答案
谢谢,我可以修复它。
ro, _, _, err := exec.ExpectSwitchCase([]expect.Caser{
&expect.Case{R: regexp.MustCompile("if you want to create a new one:"), S: "n\n", T: expect.Next(), Rt: 1},
&expect.Case{R: regexp.MustCompile(`Enter your Login Name:`), S: user + "\n", Rt: 0},
&expect.Case{R: regexp.MustCompile(`Enter your Login Name \(default root\):`), S: user + "\n", Rt: 0},
}, to)
if err != nil {
return err
}
如果如果要新建一个:如果匹配不成功,则join t状态为next,设置rt 1。
本篇关于《如何筛选出可行的案件执行方案?》的介绍就到此结束啦,但是学无止境,想要了解学习更多关于Golang的相关知识,请关注golang学习网公众号!
本文内容来源于互联网,如有侵权请联系删除。
近期热门
11.18
11.15
站内热门
最新文章
11.20
11.20
11.20
11.20
热门专题
