1
0
mirror of https://github.com/sairson/Yasso.git synced 2026-02-06 03:53:25 +08:00
Files
Yasso/pkg/netspy/example/ip_test.go

43 lines
1.0 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package example
import (
"fmt"
"net"
"strings"
"testing"
)
func Test(t *testing.T) {
inters, err := net.Interfaces()
if err != nil {
panic(err)
}
for _, inter := range inters {
// 判断网卡是否开启,过滤本地环回接口
if inter.Flags&net.FlagUp != 0 && !strings.HasPrefix(inter.Name, "lo") {
// 获取网卡下所有的地址
addrs, err := inter.Addrs()
if err != nil {
continue
}
for _, addr := range addrs {
if ipnet, ok := addr.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
//判断是否存在IPV4 IP 如果没有过滤
if strings.ContainsAny(ipnet.String(), ":") {
continue
} else {
//fmt.Println(ipnet)
minIp, maxIp := getCidrIpRange(ipnet.String())
fmt.Println("CIDR最小IP", minIp, " CIDR最大IP", maxIp)
mask, _ := ipnet.Mask.Size()
fmt.Println("掩码:", getCidrIpMask(mask))
fmt.Println("主机数量", getCidrHostNum(mask))
fmt.Println("==============================")
}
}
}
}
}
return
}