8 "github.com/Juniper/go-netconf/netconf"
9 xj "github.com/basgys/goxml2json"
10 "golang.org/x/crypto/ssh"
18 host = flag.String("host", "localhost", "Hostname")
19 username = flag.String("username", "netconf", "Username")
20 passwd = flag.String("password", "netconf", "Password")
21 source = flag.String("source", "running", "Source datastore")
22 target = flag.String("target", "running", "Target datastore")
23 subtree = flag.String("subtree", "netconf-server", "Subtree or module to select")
24 file = flag.String("file", "", "Configuration file")
25 action = flag.String("action", "get", "Netconf command: get or edit")
26 timeout = flag.Int("timeout", 30, "Timeout")
28 getConfigXml = "<get-config><source><%s/></source><filter type=\"subtree\"><%s/></filter></get-config>"
29 editConfigXml = "<edit-config><target><%s/></target><config>%s</config></edit-config>"
33 if flag.Parse(); flag.Parsed() == false {
34 log.Fatal("Syntax error!")
47 session := startSSHSession()
53 cmd := netconf.RawMethod(fmt.Sprintf(getConfigXml, *source, *subtree))
54 reply, err := session.Exec(cmd)
59 displayReply(reply.RawReply)
64 log.Fatal("Configuration file missing!")
68 session := startSSHSession()
74 if data, err := ioutil.ReadFile(*file); err == nil {
75 cmd := netconf.RawMethod(fmt.Sprintf(editConfigXml, *target, data))
76 reply, err := session.Exec(cmd)
81 displayReply(reply.RawReply)
85 func startSSHSession() *netconf.Session {
86 sshConfig := &ssh.ClientConfig{
88 Auth: []ssh.AuthMethod{ssh.Password(*passwd)},
89 HostKeyCallback: ssh.InsecureIgnoreHostKey(),
90 Timeout: time.Duration(*timeout) * time.Second,
93 session, err := netconf.DialSSH(*host, sshConfig)
101 func prettyPrint(b string) string {
103 if err := json.Indent(&out, []byte(b), "", " "); err == nil {
104 return string(out.Bytes())
109 func displayReply(rawReply string) {
110 xml := strings.NewReader(rawReply)
111 json, err := xj.Convert(xml)
113 log.Fatal("Something went sore ... XML is invalid!")
115 fmt.Println(prettyPrint(json.String()))