-
Notifications
You must be signed in to change notification settings - Fork 4
/
hostup.go
88 lines (80 loc) · 2.16 KB
/
hostup.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package jobs
import (
"fmt"
"github.com/bachittle/ping-go-v2"
"io"
"net"
"os"
"sort"
"strconv"
"strings"
"time"
)
// HostUp checks if a given host is online with ICMP echo packets.
// uses an external library (made for the purpose of this assignment) called ping-go
// check it out here: https://pkg.go.dev/github.com/bachittle/ping-go
// pass a regular domain name or CIDR name
// EX:
// - jobs.HostUp("google.com") // hostname DNS lookup
// - jobs.HostUp("192.168.2.1/24") // local CIDR lookup
//
// returns a string of hostnames that are online and offline.
func HostUp(hostname string, w io.Writer) (online []string, offline []string, err error) {
if w == nil {
w = os.Stdout
}
var ip net.IP
fmt.Println(hostname)
con := ping2.Controller{
SrcIP: net.IP{192, 168, 50, 77},
}
CIDRnum := 0
if n := strings.Index(hostname, "/"); n != -1 {
// do CIDR translation to multiple IPs
CIDRnum, err = strconv.Atoi(hostname[n+1:])
if err != nil {
return
}
ip = net.ParseIP(hostname[:n]).To4()
con.DstIPs = append(con.DstIPs, ping2.CustomIP{IP: ip, Subnet: &CIDRnum})
} else {
//var ip net.IP
ip = net.ParseIP(hostname).To4()
con.DstIPs = append(con.DstIPs, ping2.CustomIP{IP: ip, Subnet: nil})
}
fmt.Println("ip:", ip, "cidr:", CIDRnum)
con.Init()
fmt.Println("starting...")
dict := con.SendAndRecv(5 * time.Second)
fmt.Println("stopping...")
offlineMap := make(map[string]bool)
// i know this is bad but using GenerateIPs I was getting issues...
// would skip IPs unless if I checked it with fmt print, kind of like schroedingers cat
var tmpIP string
for ip := range ping2.GenerateIPs(con.DstIPs) {
if ip.String() != tmpIP {
tmpIP = ip.String()
} else {
if ip[3] == 255 {
ip[2]++
} else {
ip[3]++
}
tmpIP = ip.String()
}
if dict[ip.String()] == false {
offlineMap[ip.String()] = true
}
}
fmt.Println("n:", len(offlineMap))
fmt.Println("m:", len(dict))
for ip := range dict {
online = append(online, ip)
}
for ip := range offlineMap {
offline = append(offline, ip)
}
sort.Strings(online)
sort.Strings(offline)
return
}