forked from Juniper/go-netconf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transport_telnet.go
52 lines (42 loc) · 1.04 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
package netconf
import (
"fmt"
"github.com/ziutek/telnet"
"strings"
"time"
)
const (
TELNET_DEFAULT_PORT = 23
TELNET_TIMEOUT = 10 * time.Second
)
type VendorIOProc interface {
Login(*TransportTelnet, string, string) error
StartNetconf(*TransportTelnet) error
}
type TransportTelnet struct {
transportBasicIO
telnetConn *telnet.Conn
}
func (t *TransportTelnet) Dial(target string, username string, password string, vendor VendorIOProc) error {
if !strings.Contains(target, ":") {
target = fmt.Sprintf("%s:%d", target, TELNET_DEFAULT_PORT)
}
tn, err := telnet.Dial("tcp", target)
if err != nil {
return err
}
tn.SetUnixWriteMode(true)
t.telnetConn = tn
t.ReadWriteCloser = tn
t.chunkedFraming = false
vendor.Login(t, username, password)
vendor.StartNetconf(t)
return nil
}
func DialTelnet(target string, username string, password string, vendor VendorIOProc) (*Session, error) {
var t TransportTelnet
if err := t.Dial(target, username, password, vendor); err != nil {
return nil, err
}
return NewSession(&t), nil
}