Skip to content

Commit

Permalink
#10 change the ip module to ping all network interfaces IPs
Browse files Browse the repository at this point in the history
(before it chooses an the first net interfaces it found and ping it)
Also change the arp on windows to arp all interfaces
  • Loading branch information
mhewedy committed Aug 11, 2020
1 parent dedb251 commit a15dc84
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 54 deletions.
23 changes: 13 additions & 10 deletions ip/arp_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,21 @@ func getArpTable() []addr {

addrs := make([]addr, 0)

out, _ := command.Arp("-aN", getIP()).Call()
entries := strings.Split(out, "\n")
s, _ := command.Arp("-a").Call()
ss := strings.Split(s, "\n\r")

for _, entry := range entries[3:] {
fields := strings.Fields(entry)
if len(fields) > 1 {
addrs = append(addrs, addr{
ip: fields[0],
mac: fields[1],
})
for _, out := range ss {
entries := strings.Split(out, "\n")

for _, entry := range entries[3:] {
fields := strings.Fields(entry)
if len(fields) > 1 {
addrs = append(addrs, addr{
ip: fields[0],
mac: fields[1],
})
}
}
}

return addrs
}
21 changes: 13 additions & 8 deletions ip/ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,20 @@ func Find(vmName string, purge bool) (string, error) {
}

func ping() {

prefixes := getIPPrefixes()

var wg sync.WaitGroup
wg.Add(max)

for i := range [max]int{} {
go func(i int) {
ip := getIPPrefix() + strconv.Itoa(i)
_ = command.Ping(ip).Run()
wg.Done()
}(i)
wg.Add(max * len(prefixes))

for _, prefix := range prefixes {
for i := range [max]int{} {
go func(i int) {
ip := prefix + strconv.Itoa(i)
_ = command.Ping(ip).Run()
wg.Done()
}(i)
}
}

wg.Wait()
Expand Down
52 changes: 16 additions & 36 deletions ip/prefix.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,26 @@ import (
"strings"
)

func getIPPrefix() string {
ip := getIP()
arr := strings.Split(ip, ".")
return strings.Join(arr[:3], ".") + "."
func getIPPrefixes() []string {

ipPrefixes := make([]string, 0)

for _, ip := range getAllLocalIPAddresses() {
arr := strings.Split(ip, ".")
ipPrefixes = append(ipPrefixes, strings.Join(arr[:3], ".")+".")
}
return ipPrefixes
}

func getIP() string {
func getAllLocalIPAddresses() []string {
ifaces, err := net.Interfaces()
if err != nil {
fmt.Println(err)
return ""
return []string{}
}
strs := make([]string, 0)
ips := make([]string, 0)

for _, iface := range ifaces {

if strings.Contains(iface.Name, "VirtualBox") {
continue
}
Expand All @@ -34,35 +38,11 @@ func getIP() string {
for _, addr := range addrs {
switch v := addr.(type) {
case *net.IPNet:
ip := v.IP
strs = append(strs, ip.String())
if v.IP.To4() != nil {
ips = append(ips, v.IP.String())
}
}
}
}
for _, v := range strs {
if strings.HasPrefix(v, "192.168.") {
return v
}
}
for _, v := range strs {
if strings.HasPrefix(v, "10.") {
return v
}
}
for _, v := range strs {
if strings.HasPrefix(v, "172.") {
return v
}
}
for _, v := range strs {
if v != "127.0.0.1" && v != "::1" {
return v
}
}

if len(strs) == 0 {
return ""
}

return strs[0]
return ips
}

0 comments on commit a15dc84

Please sign in to comment.