Skip to content

Commit

Permalink
Merge pull request #46 from nitinsoniism/junos_shell_adaptation
Browse files Browse the repository at this point in the history
Junos Regex and config load fixes. Adds another example.
  • Loading branch information
carlmontanari authored Aug 29, 2021
2 parents c949ba3 + 7923eb6 commit 2a9ca5b
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 8 deletions.
2 changes: 2 additions & 0 deletions cfg/juniperjunos.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ func (p *JUNOSCfg) LoadConfig(
) ([]*base.Response, error) {
var scrapliResponses []*base.Response

p.replaceConfig = replace

// the actual value is irrelevant, if there is a key "set" w/ any value we assume user is
// loading a "set" style config
_, ok := options.Kwargs["set"]
Expand Down
17 changes: 9 additions & 8 deletions driver/core/juniperjunos.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,17 @@ func NewJUNOSDriver(
EscalatePrompt: "",
},
"shell": {
Pattern: `(?im)^%\s?$`,
Name: "shell",
PreviousPriv: execPrivLevel,
Deescalate: "exit",
Escalate: "start shell",
EscalateAuth: false,
EscalatePrompt: "",
Pattern: `(?im)^.*[%$]\s?$`,
PatternNotContains: []string{"root"},
Name: "shell",
PreviousPriv: execPrivLevel,
Deescalate: "exit",
Escalate: "start shell",
EscalateAuth: false,
EscalatePrompt: "",
},
"root_shell": {
Pattern: `(?im)^root@%\s?$`,
Pattern: `(?im)^.*root@[[:ascii:]]*:[[:ascii:]]*[%#]\s?$`,
Name: "root_shell",
PreviousPriv: execPrivLevel,
Deescalate: "exit",
Expand Down
70 changes: 70 additions & 0 deletions examples/network_driver/load_config/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package main

import (
"fmt"
"io/ioutil"

"github.com/scrapli/scrapligo/driver/base"
"github.com/scrapli/scrapligo/driver/core"
"github.com/scrapli/scrapligo/cfg"
)

//Pushes config from a file to a file on device and
//loads it to the device. Cleans up afterwords.
func main() {
d, err := core.NewCoreDriver(
"hostname",
"juniper_junos",
base.WithPort(22),
base.WithAuthUsername("root"),
base.WithAuthPassword("password"),
)

if err != nil {
fmt.Printf("failed to create driver; error: %+v\n", err)
return
}

err = d.Open()
if err != nil {
fmt.Printf("failed to open driver; error: %+v\n", err)
return
}
defer d.Close()

c, err := cfg.NewCfgDriver(
d,
"juniper_junos",
)
if err != nil {
fmt.Printf("failed to create cfg driver; error: %+v\n", err)
return
}

b, err := ioutil.ReadFile("sample.conf")
if err != nil {
fmt.Print(err)
}
fmt.Print(string(b))

prepareErr := c.Prepare()
if prepareErr != nil {
fmt.Printf("failed running prepare method: %v", prepareErr)
}

_, err = c.LoadConfig(
string(b),
false, //don't load replace. Load merge/set instead
)
if err != nil {
fmt.Printf("failed to load config; error: %+v\n", err)
return
}

_, err = c.CommitConfig()
if err != nil {
fmt.Printf("failed to commit config; error: %+v\n", err)
return
}
fmt.Printf("Done loading config\n")
}
10 changes: 10 additions & 0 deletions examples/network_driver/load_config/sample.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
interfaces {
ge-0/0/1 {
unit 0 {
family inet {
address 192.168.100.1/24;
}
}
}
}

0 comments on commit 2a9ca5b

Please sign in to comment.