Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
65 lines (59 loc) · 1.94 KB
/
Copy pathmain.go
File metadata and controls
65 lines (59 loc) · 1.94 KB
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
//go run main.go -username admin -password admin -target 172.20.20.2:6030 -destination 1.1.1.1
package main
import (
"context"
"flag"
"fmt"
"os"
"time"
log "github.com/golang/glog"
system "github.com/openconfig/gnoi/system"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
)
func checkflags(flag ...string) {
for _, f := range flag {
if f == "" {
fmt.Printf("You have an empty flag please fix.")
os.Exit(1)
}
}
}
func main() {
// Add input parameters
username := flag.String("username", "admin", "username for connection to gNOI")
password := flag.String("password", "admin", "password for connection to gNOI")
target := flag.String("target", "", "Target ip or hostname of the device running gNOI")
destination := flag.String("destination", "", "Destination of the address to ping to")
flag.Parse()
// Check for empty flags.
checkflags(*username, *password, *target, *destination)
conn, err := grpc.Dial(*target, grpc.WithInsecure())
if err != nil {
log.Exitf("Failed to %s Error: %v", target, err)
}
defer conn.Close()
// Create the new grpc service connection
Sys := system.NewSystemClient(conn)
// pass in context blank information with the timeout.
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
// cancel when the function is over.
defer cancel()
// Since Metadata needs a map to pass into the header of gRPC request create a map for it.
metamap := make(map[string]string)
// Set the username and password
metamap["username"] = *username
metamap["password"] = *password
// Set the metadata needed in the metadata package
md := metadata.New(metamap)
// set the ctx to use the metadata in every update.
ctx = metadata.NewOutgoingContext(ctx, md)
// Try to ping 10 times with a loop
for i := 0; i < 10; i++ {
response, err := Sys.Ping(ctx, &system.PingRequest{Destination: *destination})
if err != nil {
log.Fatalf("Error trying to ping: %v", err)
}
fmt.Println(response.Recv())
}
}