Use proper ping library
This commit is contained in:
parent
2caf97d535
commit
d00eb80292
3 changed files with 28 additions and 12 deletions
7
go.mod
7
go.mod
|
@ -6,3 +6,10 @@ require (
|
|||
github.com/gorilla/mux v1.8.1
|
||||
gopkg.in/yaml.v3 v3.0.1
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/digineo/go-logwrap v0.0.0-20181106161722-a178c58ea3f0 // indirect
|
||||
github.com/digineo/go-ping v1.1.0 // indirect
|
||||
golang.org/x/net v0.24.0 // indirect
|
||||
golang.org/x/sys v0.19.0 // indirect
|
||||
)
|
||||
|
|
8
go.sum
8
go.sum
|
@ -1,5 +1,13 @@
|
|||
github.com/digineo/go-logwrap v0.0.0-20181106161722-a178c58ea3f0 h1:OT/LKmj81wMymnWXaKaKBR9n1vPlu+GC0VVKaZP6kzs=
|
||||
github.com/digineo/go-logwrap v0.0.0-20181106161722-a178c58ea3f0/go.mod h1:DmqdumeAKGQNU5E8MN0ruT5ZGx8l/WbAsMbXCXcSEts=
|
||||
github.com/digineo/go-ping v1.1.0 h1:HXZPBw8/Zk+tFuHrHejBTLopcEkqK4FNn1ocqKo6xhw=
|
||||
github.com/digineo/go-ping v1.1.0/go.mod h1:rVhwm0cbn6i20vX/MBmo4OoxOvAW/6JiIf+2Oln8n0M=
|
||||
github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
|
||||
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
|
||||
golang.org/x/net v0.24.0 h1:1PcaxkF854Fu3+lvBIx5SYn9wRlBzzcnHZSiaFFAb0w=
|
||||
golang.org/x/net v0.24.0/go.mod h1:2Q7sJY5mzlzWjKtYUEXSlBWCdyaioyXzRB2RtU8KVE8=
|
||||
golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o=
|
||||
golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
|
|
25
minipam.go
25
minipam.go
|
@ -4,6 +4,7 @@ import (
|
|||
"crypto/tls"
|
||||
_ "embed"
|
||||
"encoding/json"
|
||||
"github.com/digineo/go-ping"
|
||||
"github.com/gorilla/mux"
|
||||
"gopkg.in/yaml.v3"
|
||||
"log"
|
||||
|
@ -11,7 +12,6 @@ import (
|
|||
"net/http"
|
||||
"net/netip"
|
||||
"os"
|
||||
"os/exec"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
@ -50,6 +50,7 @@ var p PersistenceT
|
|||
|
||||
//go:embed index.html
|
||||
var indexhtml []byte
|
||||
var pinger *ping.Pinger
|
||||
|
||||
func main() {
|
||||
|
||||
|
@ -80,6 +81,12 @@ func main() {
|
|||
if p.Subnets == nil {
|
||||
p.Subnets = make(map[string]SubnetT)
|
||||
}
|
||||
|
||||
pinger, err = ping.New("0.0.0.0", "")
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to create pinger: %s", err)
|
||||
}
|
||||
|
||||
go scanner()
|
||||
r := mux.NewRouter()
|
||||
r.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
|
||||
|
@ -107,16 +114,9 @@ func main() {
|
|||
|
||||
}
|
||||
|
||||
func ping(addr string) bool {
|
||||
cmd := exec.Command("ping", "-n", "-W", "0.2", "-c", "1", addr)
|
||||
cmd.Stdin = nil
|
||||
cmd.Stdout = nil
|
||||
cmd.Stderr = nil
|
||||
err := cmd.Run()
|
||||
if err == nil {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
func PingFunc(addr net.IPAddr) bool {
|
||||
_, err := pinger.PingAttempts(&addr, 200*time.Millisecond, 2)
|
||||
return err == nil
|
||||
}
|
||||
|
||||
func scanner() {
|
||||
|
@ -159,7 +159,8 @@ func scanner() {
|
|||
persistenceSubnet.TotalAddresses++
|
||||
//fmt.Println(addr.String())
|
||||
persistenceSubnet.HostList = append(persistenceSubnet.HostList, addr.String())
|
||||
pingstate := ping(addr.String())
|
||||
x := net.IPAddr{IP: net.ParseIP(addr.String())}
|
||||
pingstate := PingFunc(x)
|
||||
host, ok := persistenceSubnet.Hosts[addr.String()]
|
||||
if pingstate {
|
||||
persistenceSubnet.UsedAddresses++
|
||||
|
|
Loading…
Reference in a new issue