added ipv4 validation

This commit is contained in:
marko
2024-10-03 17:46:43 +02:00
parent 97e41eece0
commit 12f28a6623
3 changed files with 38 additions and 3 deletions

View File

@@ -4,6 +4,7 @@ import (
"fmt"
"net"
"os/exec"
"regexp"
"sync"
)
@@ -56,7 +57,16 @@ func getNetworkPrefix(ip net.IP) string {
return fmt.Sprintf("%d.%d.%d.0", ip[0], ip[1], ip[2])
}
func isValidIPv4(ip string) bool {
ipv4Regex := `^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$`
re := regexp.MustCompile(ipv4Regex)
return re.MatchString(ip)
}
func ping(ip string) bool {
if !isValidIPv4(ip) {
return false
}
output, err := exec.Command("ping", "-c", "1", "-W", "1", ip).CombinedOutput()
if err != nil {