This repository has been archived by the owner on Sep 14, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
76 lines (61 loc) · 1.48 KB
/
main.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
package main
import (
"fmt"
"os"
"strings"
"github.com/scottdware/go-junos"
log "github.com/sirupsen/logrus"
)
func main() {
err := mainInternal()
if err != nil {
log.Errorf("ERROR: %v", err)
os.Exit(1)
}
log.Info("DONE.")
}
func mainInternal() error {
log.SetLevel(log.DebugLevel)
// gather data
juniperHost := os.Getenv("JUNIPER_HOST")
if juniperHost == "" {
return fmt.Errorf("Missing environment variable: JUNIPER_HOST")
}
juniperUser := os.Getenv("JUNIPER_USER")
if juniperUser == "" {
return fmt.Errorf("Missing environment variable: JUNIPER_USER")
}
juniperPassword := os.Getenv("JUNIPER_PASSWORD")
if juniperPassword == "" {
return fmt.Errorf("Missing environment variable: JUNIPER_PASSWORD")
}
juniperCommand := os.Getenv("JUNIPER_COMMAND")
if juniperCommand == "" {
return fmt.Errorf("Missing environment variable: JUNIPER_COMMAND")
}
// perform login
log.Infof("Connecting to %v...", juniperHost)
auth := &junos.AuthMethod{
Credentials: []string{juniperUser, juniperPassword},
}
jnpr, err := junos.NewSession(juniperHost, auth)
if err != nil {
return err
}
defer jnpr.Close()
// request disconnect
commands := strings.Split(juniperCommand, ";;")
for _, cmd := range commands {
log.Infof("Running command: %v", cmd)
res, err := jnpr.Command(cmd, "text")
if err != nil {
if err.Error() == "EOF" {
log.Infof(" Result: <none>")
continue
}
return err
}
log.Infof(" Result:\n%v", strings.TrimSpace(res))
}
return nil
}