-
Notifications
You must be signed in to change notification settings - Fork 0
/
transport_telnet.go
69 lines (56 loc) · 1.74 KB
/
transport_telnet.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
package main
import (
"fmt"
"strings"
// "github.com/nemith/go-netconf/v2/transport"
"github.com/nemith/netconf/transport"
"github.com/ziutek/telnet"
)
const (
// telnetDefaultPort sets the default port for use by Telnet
telnetDefaultPort = 23
)
// VendorIOProc is the interface used when establishing a telnet NETCONF session
type VendorIOProc interface {
Login(*TransportTelnet, string, string) error
StartNetconf(*TransportTelnet) error
}
type framer = transport.Framer
type TransportTelnet = Transport
// TransportTelnet is used to define what makes up a Telnet Transport layer for
// NETCONF
// type TransportTelnet struct {
type Transport struct {
telnetConn *telnet.Conn
*framer
}
// Dial is used to create a TCP Telnet connection to the remote host returning
// only an error if it is unable to dial the remote host.
func (t *TransportTelnet) Dial(target string, username string, password string, vendor VendorIOProc) error {
if !strings.Contains(target, ":") {
target = fmt.Sprintf("%s:%d", target, telnetDefaultPort)
}
tn, err := telnet.Dial("tcp", target)
if err != nil {
return err
}
tn.SetUnixWriteMode(true)
t.telnetConn = tn
// t.ReadWriteCloser = tn
// t.FramedTransport = transport.NewFramedTransport(tn, tn)
t.framer = transport.NewFramer(tn, tn)
// vendor.Login(t, username, password)
// vendor.StartNetconf(t)
return nil
}
func (t *TransportTelnet) Close() error {
return t.telnetConn.Close()
}
// DialTelnet dials and returns the usable telnet session.
func DialTelnet(target string, username string, password string, vendor VendorIOProc) (transport.Transport, error) {
var t *TransportTelnet = &TransportTelnet{}
if err := t.Dial(target, username, password, vendor); err != nil {
return nil, err
}
return t, nil
}