-
Notifications
You must be signed in to change notification settings - Fork 5
/
huawei.go
54 lines (50 loc) · 1.95 KB
/
huawei.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
/*
Package huawei implements huawei CLI using genericcli.
*/
package huawei
import (
"github.com/annetutil/gnetcli/pkg/cmd"
"github.com/annetutil/gnetcli/pkg/device/genericcli"
"github.com/annetutil/gnetcli/pkg/expr"
"github.com/annetutil/gnetcli/pkg/streamer"
)
const (
loginExpression = `.*Username:$`
questionExpression = `\n(?P<question>.*Continue\? \[Y/N\]:)$`
promptExpression = `(\r\n|^)(?P<prompt>(<[\w\-]+>|\[[~*]?[/\w\-.:]+\]))$`
errorExpression = `(` +
`\^\r\nError: (?P<error>.+) at '\^' position\.` +
`|Error: You do not have permission to run the command or the command is incomplete` +
`|\ +\^\nError(?:\[\d+\])?:\s*(?P<msg>.*?) found at '\^' position.` +
`|^Error:\s*(?P<msg>(No|You do not have) permission.*)` +
`|Error(?:\[\d+\])?:\s*(?P<msg>.+?)` +
`)`
passwordExpression = `(\r\n|^)Password:$`
passwordErrorExpression = `.*Error: Username or password error\.\r\n$|.*Authentication fail(\x00\r\n)?$|Error: The password is invalid.\r\n$`
pagerExpression = `(?P<store>(\r\n|\n))? ---- More ----$`
)
var autoCommands = []cmd.Cmd{
cmd.NewCmd("screen-length 0 temporary", cmd.WithErrorIgnore()),
cmd.NewCmd("terminal echo-mode line", cmd.WithErrorIgnore()),
}
func NewDevice(connector streamer.Connector, opts ...genericcli.GenericDeviceOption) genericcli.GenericDevice {
cli := genericcli.MakeGenericCLI(
expr.NewSimpleExprLast200(promptExpression),
expr.NewSimpleExprLast200(errorExpression),
genericcli.WithLoginExprs(
expr.NewSimpleExprLast200(loginExpression),
expr.NewSimpleExprLast200(passwordExpression),
expr.NewSimpleExprLast200(passwordErrorExpression),
),
genericcli.WithPager(
expr.NewSimpleExprLast200(pagerExpression),
),
genericcli.WithAutoCommands(autoCommands),
genericcli.WithQuestion(
expr.NewSimpleExprLast200(questionExpression),
),
genericcli.WithSFTPEnabled(),
genericcli.WithTerminalParams(400, 0),
)
return genericcli.MakeGenericDevice(cli, connector, opts...)
}