-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
session.go
214 lines (183 loc) · 5.61 KB
/
session.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package junos
import (
"fmt"
"log"
"os"
"path"
"strconv"
"time"
)
const directoryPermission = 0o755
// Session information to connect on Junos Device and more.
type Session struct {
junosFakeUpdateAlso bool
junosFakeDeleteAlso bool
junosPort int
junosSleepLock int
junosSleepShort int
junosSleepSSHClosed int
junosFilePermission int64
junosIP string
junosUserName string
junosPassword string
junosSSHKeyPEM string
junosSSHKeyFile string
junosKeyPass string
junosGroupIntDel string
junosLogFile string
junosFakeCreateSetFile string
junosSSHCiphers []string
}
func (sess *Session) startNewSession() (*NetconfObject, error) {
var auth netconfAuthMethod
auth.Username = sess.junosUserName
auth.Ciphers = sess.junosSSHCiphers
if sess.junosSSHKeyPEM != "" {
auth.PrivateKeyPEM = sess.junosSSHKeyPEM
if sess.junosKeyPass != "" {
auth.Passphrase = sess.junosKeyPass
}
}
if sess.junosSSHKeyFile != "" {
auth.PrivateKeyFile = sess.junosSSHKeyFile
if sess.junosKeyPass != "" {
auth.Passphrase = sess.junosKeyPass
}
}
if sess.junosPassword != "" {
auth.Password = sess.junosPassword
}
jnpr, err := netconfNewSession(sess.junosIP+":"+strconv.Itoa(sess.junosPort), &auth)
if err != nil {
return nil, err
}
if jnpr.SystemInformation.HardwareModel == "" {
return jnpr, fmt.Errorf("can't read model of device with <get-system-information/> netconf command")
}
sess.logFile("[startNewSession] started")
return jnpr, nil
}
func (sess *Session) closeSession(jnpr *NetconfObject) {
err := jnpr.close(sess.junosSleepSSHClosed)
if err != nil {
sess.logFile(fmt.Sprintf("[closeSession] err: %q", err))
} else {
sess.logFile("[closeSession] closed")
}
}
func (sess *Session) command(cmd string, jnpr *NetconfObject) (string, error) {
read, err := jnpr.netconfCommand(cmd)
sess.logFile(fmt.Sprintf("[command] cmd: %q", cmd))
sess.logFile(fmt.Sprintf("[command] read: %q", read))
sleepShort(sess.junosSleepShort)
if err != nil && read != emptyWord {
sess.logFile(fmt.Sprintf("[command] err: %q", err))
return "", err
}
return read, nil
}
func (sess *Session) commandXML(cmd string, jnpr *NetconfObject) (string, error) {
read, err := jnpr.netconfCommandXML(cmd)
sess.logFile(fmt.Sprintf("[commandXML] cmd: %q", cmd))
sess.logFile(fmt.Sprintf("[commandXML] read: %q", read))
sleepShort(sess.junosSleepShort)
if err != nil {
sess.logFile(fmt.Sprintf("[commandXML] err: %q", err))
return "", err
}
return read, nil
}
func (sess *Session) configSet(cmd []string, jnpr *NetconfObject) error {
if jnpr != nil {
message, err := jnpr.netconfConfigSet(cmd)
sleepShort(sess.junosSleepShort)
sess.logFile(fmt.Sprintf("[configSet] cmd: %q", cmd))
sess.logFile(fmt.Sprintf("[configSet] message: %q", message))
if err != nil {
sess.logFile(fmt.Sprintf("[configSet] err: %q", err))
return err
}
return nil
} else if sess.junosFakeCreateSetFile != "" {
return sess.appendFakeCreateSetFile(cmd)
}
return fmt.Errorf("internal error: sess.configSet call without connection on device")
}
func (sess *Session) appendFakeCreateSetFile(lines []string) error {
dirSetFile := path.Dir(sess.junosFakeCreateSetFile)
if _, err := os.Stat(dirSetFile); err != nil {
if err := os.MkdirAll(dirSetFile, os.FileMode(directoryPermission)); err != nil {
return fmt.Errorf("failed to create parent directory of `%s` : %w", sess.junosFakeCreateSetFile, err)
}
}
f, err := os.OpenFile(sess.junosFakeCreateSetFile,
os.O_APPEND|os.O_CREATE|os.O_WRONLY, os.FileMode(sess.junosFilePermission))
if err != nil {
return fmt.Errorf("failed to openfile `%s` : %w", sess.junosFakeCreateSetFile, err)
}
defer f.Close()
for _, v := range lines {
if _, err := f.WriteString(v + "\n"); err != nil {
return fmt.Errorf("failed to write in file `%s` : %w", sess.junosFakeCreateSetFile, err)
}
}
return nil
}
func (sess *Session) commitConf(logMessage string, jnpr *NetconfObject) (_warnings []error, _err error) {
sess.logFile(fmt.Sprintf("[commitConf] commit %q", logMessage))
warns, err := jnpr.netconfCommit(logMessage)
sleepShort(sess.junosSleepShort)
if len(warns) > 0 {
for _, w := range warns {
sess.logFile(fmt.Sprintf("[commitConf] commit warning: %q", w))
}
}
if err != nil {
sess.logFile(fmt.Sprintf("[commitConf] commit error: %q", err))
return warns, err
}
return warns, nil
}
func (sess *Session) configLock(jnpr *NetconfObject) {
var lock bool
for {
lock = jnpr.netconfConfigLock()
if lock {
sess.logFile("[configLock] locked")
sleepShort(sess.junosSleepShort)
break
} else {
sess.logFile("[configLock] sleep for wait lock")
sleep(sess.junosSleepLock)
}
}
}
func (sess *Session) configClear(jnpr *NetconfObject) (errs []error) {
errs = append(errs, jnpr.netconfConfigClear()...)
sleepShort(sess.junosSleepShort)
sess.logFile("[configClear] config clear")
errs = append(errs, jnpr.netconfConfigUnlock()...)
sleepShort(sess.junosSleepShort)
sess.logFile("[configClear] config unlock")
return
}
// log message in junosLogFile.
func (sess *Session) logFile(message string) {
if sess.junosLogFile != "" {
f, err := os.OpenFile(sess.junosLogFile,
os.O_WRONLY|os.O_CREATE|os.O_APPEND, os.FileMode(sess.junosFilePermission))
if err != nil {
log.Fatal(err)
}
defer f.Close()
log.SetOutput(f)
log.SetPrefix(time.Now().Format("2006-01-02 15:04:05"))
log.Printf("%s", message)
}
}
func sleep(timeSleep int) {
time.Sleep(time.Duration(timeSleep) * time.Second)
}
func sleepShort(timeSleep int) {
time.Sleep(time.Duration(timeSleep) * time.Millisecond)
}