forked from Juniper/go-netconf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ioproc.go
61 lines (50 loc) · 1.21 KB
/
ioproc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package jnpr
import (
"fmt"
"regexp"
"github.com/Juniper/go-netconf/netconf"
)
const (
netconfCMDCLI = "junoscript netconf need-trailer"
netconfCMDShell = "exec xml-mode netconf need-trailer"
)
const (
// CLIModeShell tells the system what mode to open the shell in
CLIModeShell = iota
// CLIModeCLI sets (no) mode for the CLI
CLIModeCLI
)
type JnprIOProc struct {
cliMode int
}
var promptRE = regexp.MustCompile(`([>%])\s+`)
func (j *JnprIOProc) Login(t *netconf.TransportTelnet, username string, password string) error {
t.WaitForString("ogin:")
t.Writeln([]byte(username))
t.WaitForString("assword:")
t.Writeln([]byte(password))
_, prompt, err := t.WaitForRegexp(promptRE)
if err != nil {
return err
}
switch string(prompt[0]) {
case ">":
j.cliMode = CLIModeCLI
case "%":
j.cliMode = CLIModeShell
default:
return fmt.Errorf("Cannot determine prompt '%s'", prompt[0])
}
return nil
}
func (j *JnprIOProc) StartNetconf(t *netconf.TransportTelnet) error {
switch j.cliMode {
case CLIModeShell:
t.Writeln([]byte(netconfCMDShell))
return nil
case CLIModeCLI:
t.Writeln([]byte(netconfCMDCLI))
return nil
}
return fmt.Errorf("Cannot start netconf: Unknown CLI mode '%d'", j.cliMode)
}