2017-09-17 03:26:19 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2018-04-20 08:56:06 +02:00
|
|
|
"encoding/json"
|
2017-09-17 03:26:19 +02:00
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/FreifunkBremen/yanic/respond"
|
|
|
|
"github.com/FreifunkBremen/yanic/runtime"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
var wait int
|
|
|
|
|
|
|
|
// queryCmd represents the query command
|
|
|
|
var queryCmd = &cobra.Command{
|
|
|
|
Use: "query <interface> <destination>",
|
|
|
|
Short: "Sends a query on the interface to the destination and waits for a response",
|
2017-10-01 11:13:26 +02:00
|
|
|
Example: `yanic query wlan0 "fe80::eade:27ff:dead:beef"`,
|
2017-09-17 03:26:19 +02:00
|
|
|
Args: cobra.ExactArgs(2),
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
iface := args[0]
|
2017-10-01 11:13:26 +02:00
|
|
|
dstAddress := net.ParseIP(args[1])
|
2017-09-17 03:26:19 +02:00
|
|
|
|
|
|
|
log.Printf("Sending request address=%s iface=%s", dstAddress, iface)
|
|
|
|
|
2018-01-07 21:00:56 +01:00
|
|
|
nodes := runtime.NewNodes(&runtime.NodesConfig{})
|
2017-09-17 03:26:19 +02:00
|
|
|
|
2017-11-21 15:12:06 +01:00
|
|
|
collector := respond.NewCollector(nil, nodes, []string{}, []string{iface}, 0)
|
2017-09-17 03:26:19 +02:00
|
|
|
defer collector.Close()
|
2017-10-01 11:13:26 +02:00
|
|
|
collector.SendPacket(dstAddress)
|
2017-09-17 03:26:19 +02:00
|
|
|
|
|
|
|
time.Sleep(time.Second * time.Duration(wait))
|
|
|
|
|
|
|
|
for id, data := range nodes.List {
|
2018-04-20 08:56:06 +02:00
|
|
|
bytes, err := json.Marshal(data)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("%s: %+v", id, err)
|
|
|
|
} else {
|
|
|
|
log.Printf("%s: %+v", id, string(bytes))
|
|
|
|
}
|
2017-09-17 03:26:19 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
RootCmd.AddCommand(queryCmd)
|
|
|
|
queryCmd.Flags().IntVar(&wait, "wait", 1, "Seconds to wait for a response")
|
|
|
|
}
|