forked from m3philis/junos_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpc_client.go
42 lines (32 loc) · 849 Bytes
/
rpc_client.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
package rpc
import (
"encoding/xml"
"fmt"
"log"
"github.com/czerwonk/junos_exporter/connector"
)
// Client sends commands to JunOS and parses results
type Client struct {
conn *connector.SSHConnection
debug bool
}
// NewClient creates a new client to connect to
func NewClient(ssh *connector.SSHConnection, debug bool) *Client {
rpc := &Client{conn: ssh, debug: debug}
return rpc
}
// RunCommandAndParse runs a command on JunOS and unmarshals the XML result
func (c *Client) RunCommandAndParse(cmd string, obj interface{}) error {
if c.debug {
log.Printf("Running command on %s: %s\n", c.conn.Host, cmd)
}
b, err := c.conn.RunCommand(fmt.Sprintf("%s | display xml", cmd))
if err != nil {
return err
}
if c.debug {
log.Printf("Output for %s: %s\n", c.conn.Host, string(b))
}
err = xml.Unmarshal(b, obj)
return err
}