-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtmsh.go
122 lines (97 loc) · 2.49 KB
/
tmsh.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package tmsh
import (
"bufio"
"bytes"
"fmt"
"strings"
)
// BigIP is a struct for session state
type BigIP struct {
host string
user string
sshconn SSH
}
// NewKeySession is NewSession plus key handling
func NewKeySession(host, port, user string, key []byte) (*BigIP, error) {
return GenSession(host, port, user, "", key)
}
// NewSession sets up new SSH session to BIG-IP TMSH
func NewSession(host, port, user, password string) (*BigIP, error) {
return GenSession(host, port, user, password, []byte{})
}
// GenSession handles either Password or SSH Key based..
func GenSession(host, port, user, password string, key []byte) (*BigIP, error) {
sshconn, err := newSSHConnection(host+":"+port, user, password, key)
if err != nil {
return nil, err
}
ret, _ := sshconn.Recv("# ")
if !strings.Contains(string(ret), "(tmos)") {
_, err := sshconn.Send("tmsh")
if err != nil {
return nil, err
}
ret, err = sshconn.Recv("# ")
if err != nil {
return nil, err
}
}
bigip := &BigIP{
host: host,
user: user,
sshconn: sshconn,
}
// Suppress pager output
if _, err := bigip.ExecuteCommand("modify cli preference pager disabled display-threshold 0"); err != nil {
return nil, err
}
return bigip, nil
}
// ExecuteCommand is used to execute any TMSH commands
func (bigip *BigIP) ExecuteCommand(cmd string) (string, error) {
promptSuffix := "# "
_, err := bigip.sshconn.Send(cmd)
if err != nil {
return "", err
}
results, err := bigip.sshconn.Recv(promptSuffix)
if err != nil {
return "", err
}
results = removeCarriageReturn(results)
reader := bytes.NewReader(results)
scanner := bufio.NewScanner(reader)
var lines []string
for scanner.Scan() {
text := scanner.Text()
line := removeSpaceAndBackspace(text)
if strings.HasPrefix(line, "Last login:") ||
strings.Contains(line, "(tmos)") ||
strings.HasPrefix(line, cmd) {
continue
}
lines = append(lines, text)
}
return strings.Join(lines, "\n"), nil
}
// Save is used to execute 'save /sys config' command
func (bigip *BigIP) Save() error {
ret, err := bigip.ExecuteCommand("save /sys config current-partition")
if err != nil {
fmt.Errorf(ret)
}
if strings.Contains(ret, "Syntax Error: \"current-partition\" unknown property") {
ret, err = bigip.ExecuteCommand("save /sys config")
if err != nil {
fmt.Errorf(ret)
}
}
if strings.Contains(ret, "Error") {
return fmt.Errorf(ret)
}
return nil
}
// Close is used to close SSH session
func (bigip *BigIP) Close() {
bigip.sshconn.Close()
}