forked from AppliedTrust/sweet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cisco.go
70 lines (61 loc) · 2.16 KB
/
cisco.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
62
63
64
65
66
67
68
69
70
package sweet
import (
"fmt"
"strings"
)
type Cisco struct {
}
func newCiscoCollector() Collector {
return Cisco{}
}
func (collector Cisco) Collect(device DeviceConfig) (map[string]string, error) {
result := make(map[string]string)
c, err := newSSHCollector(device)
if err != nil {
return result, fmt.Errorf("Error connecting to %s: %s", device.Hostname, err.Error())
}
if err := expect("assword:", c.Receive); err != nil {
return result, fmt.Errorf("Missing password prompt: %s", err.Error())
}
c.Send <- device.Config["pass"] + "\n"
multi := []string{"#", ">", "assword:"}
m, err := expectMulti(multi, c.Receive)
if err != nil {
return result, fmt.Errorf("Invalid response to password: %s", err.Error())
}
if m == "assword:" {
return result, fmt.Errorf("Bad username or password.")
} else if m == ">" {
c.Send <- "enable\n"
if err := expect("assword:", c.Receive); err != nil {
return result, fmt.Errorf("Missing enable password prompt: %s", err.Error())
}
c.Send <- device.Config["enable"] + "\n"
if err := expect("#", c.Receive); err != nil {
return result, fmt.Errorf("Enable attempt failed: %s", err.Error())
}
}
c.Send <- "terminal length 0\n"
if err := expect("#", c.Receive); err != nil {
return result, fmt.Errorf("Command 'terminal length 0' failed: %s", err.Error())
}
c.Send <- "terminal pager 0\n"
if err := expect("#", c.Receive); err != nil {
return result, fmt.Errorf("Command 'terminal pager 0' failed: %s", err.Error())
}
c.Send <- "show running-config\n"
result["config"], err = expectSaveTimeout("#", c.Receive, device.CommandTimeout)
if err != nil {
return result, fmt.Errorf("Command 'show running-config' failed: %s", err.Error())
}
c.Send <- "show version\n"
result["version"], err = expectSaveTimeout("#", c.Receive, device.CommandTimeout)
if err != nil {
return result, fmt.Errorf("Command 'show version' failed: %s", err.Error())
}
// cleanup config results
result["config"] = strings.TrimSpace(strings.TrimPrefix(result["config"], "show running-config"))
result["config"] = strings.TrimSpace(strings.TrimPrefix(result["config"], "Building configuration..."))
c.Send <- "exit\n"
return result, nil
}