-
Notifications
You must be signed in to change notification settings - Fork 0
/
gnetco.go
180 lines (146 loc) · 3.72 KB
/
gnetco.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
package gnetco
import (
"bytes"
"fmt"
"io"
"log"
"time"
"golang.org/x/crypto/ssh"
)
const enderString = "]]>]]>"
type GNetCo interface {
connect(string, string, string) error
Exec([]byte) ([]byte, error)
Lock(func([]byte) ([]byte, error), []byte) ([]byte, error)
unlock()
Disconnect()
TimeMe(func([]byte) ([]byte, error), []byte) time.Duration
}
type netconf struct {
reader io.Reader
writer io.Writer
session *ssh.Session
}
func Connect(socket, username, password string) (GNetCo, error) {
n := new(netconf)
if err := n.connect(socket, username, password); err != nil {
return nil, fmt.Errorf("connection error %s", err)
}
rpcHelloString := `<?xml version="1.0" encoding="UTF-8"?>
<hello xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
<capabilities>
<capability>urn:ietf:params:netconf:base:1.0</capability>
</capabilities>
</hello>`
n.Exec([]byte(rpcHelloString))
// if reply, err := n.Exec([]byte(rpcHelloString)); err != nil {
// return nil, fmt.Errorf("%s", err)
// // fmt.Println(string(reply))
// } else {
// fmt.Println(string(reply))
// }
return n, nil
}
func (n *netconf) connect(socket, username, password string) error {
credentials := &ssh.ClientConfig{
User: username, //"netconf-user",
Auth: []ssh.AuthMethod{ssh.Password(password /*"netconf-password"*/)},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
sshClient, err := ssh.Dial("tcp", socket, credentials)
if err != nil {
return fmt.Errorf("ssh client dial %s", err)
}
n.session, err = sshClient.NewSession()
if err != nil {
return fmt.Errorf("ssh client %s", err)
}
n.writer, err = n.session.StdinPipe()
if err != nil {
return fmt.Errorf("writer error %s", err)
}
n.reader, err = n.session.StdoutPipe()
if err != nil {
return fmt.Errorf("reader error %s", err)
}
n.session.RequestSubsystem("netconf")
return nil
}
func (n *netconf) Exec(rpcMesage []byte) ([]byte, error) {
if err := n.send([]byte(rpcMesage)); err != nil {
return nil, err
}
reply, err := n.receive()
if err != nil {
return nil, err
}
return reply, nil
}
func (n *netconf) TimeMe(f func([]byte) ([]byte, error), rpcMessage []byte) time.Duration {
startTime := time.Now()
f(rpcMessage)
duration := time.Since(startTime)
return duration
}
func (n *netconf) Lock(f func([]byte) ([]byte, error), rpcMessage []byte) ([]byte, error) {
lockMessage := `<?xml version="1.0" encoding="UTF-8"?>
<rpc message-id="101" xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
<lock>
<target>
<running/>
</target>
</lock>
</rpc>`
n.Exec([]byte(lockMessage))
defer n.unlock()
return f(rpcMessage)
}
func (n *netconf) unlock() {
unlockMessage := `<?xml version="1.0" encoding="UTF-8"?>
<rpc message-id="100" xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
<unlock>
<target>
<running/>
</target>
</unlock>
</rpc>`
n.Exec([]byte(unlockMessage))
}
func (n *netconf) Disconnect() {
n.session.Close()
}
func (n *netconf) send(message []byte) error {
message = append(message, []byte(enderString)...)
_, err := n.writer.Write(message)
if err != nil {
return fmt.Errorf("writer error %s", err)
}
return nil
}
func (n *netconf) receive() ([]byte, error) {
var receiveBytesBuffer bytes.Buffer
buf := make([]byte, 8192)
pos := 0
for {
n, err := n.reader.Read(buf[pos : pos+(len(buf)/2)])
if err != nil {
if err != io.EOF {
log.Fatalf("reader error %s", err)
}
break
}
if n > 0 {
end := bytes.Index(buf[0:pos+n], []byte(enderString))
if end > -1 {
receiveBytesBuffer.Write(buf[0:end])
return receiveBytesBuffer.Bytes(), nil
}
if pos > 0 {
receiveBytesBuffer.Write(buf[0:pos])
copy(buf, buf[pos:pos+n])
}
pos = n
}
}
return nil, fmt.Errorf("receive failed")
}