-
Notifications
You must be signed in to change notification settings - Fork 0
/
hosts.go
47 lines (36 loc) · 852 Bytes
/
hosts.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
package main
import (
"context"
"fmt"
"os"
"os/signal"
"time"
"github.com/vokomarov/netshark/scanner/host"
)
type hostsCommand struct {
Timeout int `short:"t" long:"timeout" default:"5" description:"Timeout in seconds to wait for ARP responses."`
}
// Execute will run the command
func (c *hostsCommand) Execute(_ []string) error {
fmt.Printf("Scanning Hosts..\n")
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt)
scanner := host.NewScanner()
_, stopFunc := scanner.Ctx(context.Background())
go scanner.Scan()
go func() {
timeout := time.NewTicker(time.Duration(c.Timeout) * time.Second)
select {
case <-timeout.C:
stopFunc()
return
case <-quit:
stopFunc()
return
}
}()
for h := range scanner.Hosts() {
fmt.Printf(" [IP: %s] \t [MAC: %s] \n", h.IP, h.MAC)
}
return scanner.Error
}