/
device.go
44 lines (36 loc) · 991 Bytes
/
device.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
package connector
import (
"io"
"github.com/slashdoom/aruba_exporter/config"
"golang.org/x/crypto/ssh"
)
type Device struct {
Host string
Port string
Auth AuthMethod
ClientConfig ssh.ClientConfig
DeviceConfig *config.DeviceConfig
}
// AuthMethod is the method to use to authenticate agaist the device
type AuthMethod func(*ssh.ClientConfig)
// AuthByPassword uses password authentication
func AuthByPassword(username, password string) AuthMethod {
return func(cfg *ssh.ClientConfig) {
cfg.User = username
cfg.Auth = append(cfg.Auth, ssh.Password(password))
}
}
// AuthByKey uses public key authentication
func AuthByKey(username string, key io.Reader) (AuthMethod, error) {
pk, err := loadPrivateKey(key)
if err != nil {
return nil, err
}
return func(cfg *ssh.ClientConfig) {
cfg.User = username
cfg.Auth = append(cfg.Auth, pk)
}, nil
}
func (d *Device) String() string {
return d.Host
}