/
lldp_neighbors.go
70 lines (62 loc) · 1.61 KB
/
lldp_neighbors.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
package cmd
import (
"fmt"
"github.com/RackHD/inservice/plugins/lldp/grpc/lldp"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"golang.org/x/net/context"
"google.golang.org/grpc"
"io"
"time"
)
// neighborsCmd represents the neighbors command
var neighborsCmd = &cobra.Command{
Use: "neighbors",
Short: "Show information on the LLDP neighbors",
Long: `Displays list of neighbors seen by LLDP Plugin or those seen from specific interface:
Usage:
Neighbor List - inservice-cli lldp neighbors list
Interface Neighbors - inservice-cli lldp neighbors <name>`,
RunE: func(cmd *cobra.Command, args []string) error {
err := loadViperConfig()
if err != nil {
return fmt.Errorf("Could not load viper config: %s", err)
}
LLDPPort := viper.GetInt("plugins.inservice-lldp.port")
if len(args) != 1 {
return fmt.Errorf("Interface Name or [command] Required")
}
conn, err := grpc.Dial(
fmt.Sprintf("%s:%d", AgentHost, LLDPPort),
grpc.WithInsecure(),
grpc.WithTimeout(time.Duration(AgentTimeout)*time.Second),
)
if err != nil {
return fmt.Errorf("Unable to connect to host: %s", err)
}
defer conn.Close()
client := lldp.NewLldpClient(conn)
stream, err := client.ListInterfaceNeighbors(
context.Background(),
&lldp.Interface{
Name: args[0],
},
)
if err != nil {
return fmt.Errorf("Unable to list neighbors: %s", err)
}
for {
item, err := stream.Recv()
if err == io.EOF {
return nil
}
if err != nil {
return fmt.Errorf("Unable to stream neighbors: %s", err)
}
fmt.Printf("%+v\n", item)
}
},
}
func init() {
lldpCmd.AddCommand(neighborsCmd)
}