How to use ParseCIDR method of lib Package

Best K6 code snippet using lib.ParseCIDR

helpers_windows.go

Source:helpers_windows.go Github

copy

Full Screen

...53 }54 return int(ret), err55}56func AddIncludeTableEntry(cidr string) (int, error) {57 ip, ipm, err := net.ParseCIDR(cidr)58 if err != nil {59 return -1, err60 }61 ret, _, callErr := syscall.Syscall(ptrAddIncludeTableEntry, 2, uintptr(ipToInt(ip)), uintptr(ipToInt(net.IP(ipm.Mask))), 0)62 if callErr != 0 {63 return -1, callErr64 }65 return int(ret), err66}67func AddExcludeTableEntry(cidr string) (int, error) {68 ip, ipm, err := net.ParseCIDR(cidr)69 if err != nil {70 return -1, err71 }72 ret, _, callErr := syscall.Syscall(ptrAddExcludeTableEntry, 2, uintptr(ipToInt(ip)), uintptr(ipToInt(net.IP(ipm.Mask))), 0)73 if callErr != 0 {74 return -1, callErr75 }76 return int(ret), err77}78func DeleteTableEntry(cidr string) (int, error) {79 ip, ipm, err := net.ParseCIDR(cidr)80 if err != nil {81 return -1, err82 }83 ret, _, callErr := syscall.Syscall(ptrDeleteTableEntry, 2, uintptr(ipToInt(ip)), uintptr(ipToInt(net.IP(ipm.Mask))), 0)84 if callErr != 0 {85 return -1, callErr86 }87 return int(ret), err88}89func GB2312toUTF8(s []byte) []byte {90 d, _ := simplifiedchinese.GBK.NewDecoder().Bytes(s)91 return d92}93func getWaterConfig() water.Config {94 return water.Config{95 DeviceType: water.TUN,96 PlatformSpecificParams: water.PlatformSpecificParams{97 ComponentID: "tap0901",98 Network: "10.1.0.10/24",99 },100 }101}102func (o *VPNConnector) exec(command string, args []string) error {103 cmd := exec.Command(command, args...)104 log.Println(command, args)105 buf, err := cmd.Output()106 if err != nil {107 log.Println("Command failed: ", string(GB2312toUTF8(buf)))108 }109 log.Println(string(GB2312toUTF8(buf)))110 return err111}112func (o *VPNConnector) setDeviceNetworkParameters(iName string, address string, gatway string, mtu string) error {113 ip, mask, err := net.ParseCIDR(address)114 if err != nil {115 return err116 }117 //netsh interface ip set address name=”本地连接” source=static addr=192.168.0.3 mask=255.255.255.0 gateway=192.168.0.1 /32118 args := []string{"interface", "ipv4", "set", "address", "name=" + iName, "source=static",119 "addr=" + ip.String(),120 "mask=" + net.IP(mask.Mask).String(),121 "gateway=" + gatway,122 }123 err = o.exec("netsh", args)124 if err != nil {125 return err126 }127 //netsh interface ipv4 set subinterface "本地连接" mtu=1480 store=persistent...

Full Screen

Full Screen

network.go

Source:network.go Github

copy

Full Screen

...19 })20 return21}22func IntersectEachOther(cidrA, cidrB string) (bool, error) {23 ipA, ipNetA, errA := net.ParseCIDR(cidrA)24 if errA != nil {25 return true, errA26 }27 ipB, ipNetB, errB := net.ParseCIDR(cidrB)28 if errB != nil {29 return true, errB30 }31 return ipNetA.Contains(ipB) || ipNetB.Contains(ipA), nil32}33func OpenPortAndListen(ip net.IP, port int, zone string, stopChan <-chan struct{}) {34 ln, err := net.Listen("tcp", fmt.Sprintf(":%d", port))35 if err != nil {36 panic(err)37 }38 go func(ln net.Listener) {39 for {40 conn, _ := ln.Accept()41 conn.Close()42 }43 }(ln)44 <-stopChan45 ln.Close()46}47func GetDefaultGateway(ipv4 bool) (string, *net.Interface, error) {48 // run command "ip r | grep -i 'default via'"49 var command *exec.Cmd50 if ipv4 {51 command = exec.Command("ip", "r")52 } else {53 // might broken on grep version 2.2054 command = exec.Command("ip", "-6", "r")55 }56 cmdGrepDefaultGW := exec.Command("grep", "default via")57 output, stderr, err := cmdLib.CmdPipeline(command, cmdGrepDefaultGW)58 if err != nil {59 log.Debug(fmt.Sprintf("Failed to get default gateway due to error: \"%s\"", stderr))60 return "", nil, err61 }62 nicInfo := strings.Split(string(output), " ")63 if len(nicInfo) < 4 {64 return "", nil, errors.New(fmt.Sprintf("Failed to get gateway info by issue \"%s\"", command.String()))65 }66 interFaceName := nicInfo[4]67 defaultGw := nicInfo[2]68 nic, errNic := net.InterfaceByName(interFaceName)69 if errNic != nil {70 return "", nil, errNic71 }72 return defaultGw, nic, nil73}74func GetDefaultIP(ipv4 bool) net.IP {75 _, nic, err := GetDefaultGateway(ipv4)76 if err != nil {77 log.Fatal(err)78 }79 addresses, errIp := nic.Addrs()80 if errIp != nil {81 log.Fatal(errIp)82 }83 ip, _, errCidr := net.ParseCIDR(addresses[0].String())84 if errCidr != nil {85 log.Fatal(errCidr)86 }87 if ipv4 {88 return ip.To4()89 } else {90 return ip.To16()91 }92}93func GetNicWithCIDR(cidr string) (*net.Interface, error) {94 var stdOut, stdErr bytes.Buffer95 var err error96 interfaceName := ""97 ipv4 := true98 if ip, _, err := net.ParseCIDR(cidr); err != nil {99 return nil, err100 } else {101 ipv4 = !strings.Contains(ip.String(), ":")102 }103 if ipv4 {104 stdOut, stdErr, err = cmdLib.RunCmd("ip", "r")105 } else {106 stdOut, stdErr, err = cmdLib.RunCmd("ip", "-6", "r")107 }108 if err != nil {109 log.Error(fmt.Sprintf("Failed to get nic information by input CIDR \"%s\" due to error \"%s\" ", cidr, stdErr.String()))110 return nil, err111 }112 routes := strings.Split(stdOut.String(), "\n")...

Full Screen

Full Screen

routes_linux.go

Source:routes_linux.go Github

copy

Full Screen

...24 return errors.Wrapf(err, "[%v] function netlink.RouteGet()", errors.Trace())25 }26 for ipDst := range r.routes.local {27 r.routes.local[ipDst] = false28 _, dst, err := net.ParseCIDR(ipDst.string())29 if err != nil {30 return errors.Wrapf(err, "[%v] function net.ParseCIDR()", errors.Trace())31 }32 route := &netlink.Route{33 LinkIndex: ifcLink.Attrs().Index,34 Dst: dst,35 }36 for _, sysRoute := range sysRouteList {37 if route.Dst.String() == sysRoute.Dst.String() && route.LinkIndex == sysRoute.LinkIndex {38 // route is configured39 r.routes.local[ipDst] = true40 }41 }42 }43 return nil44}45func (ni *networkInterface) routeAdd(ipDst cidrIPDst) error {46 r := ipDst.string()47 if len(r) == 0 {48 return nil49 }50 ifcLink, err := netlink.LinkByName(ni.devName())51 if err != nil {52 return errors.Wrapf(err, "[%v] function netlink.LinkByName()", errors.Trace())53 }54 _, dst, err := net.ParseCIDR(r)55 if err != nil {56 return errors.Wrapf(err, "[%v] function net.ParseCIDR()", errors.Trace())57 }58 route := &netlink.Route{59 LinkIndex: ifcLink.Attrs().Index,60 Dst: dst,61 }62 if err := netlink.RouteAdd(route); err != nil {63 return errors.Wrapf(err, "[%v] function netlink.RouteAdd()", errors.Trace())64 }65 xlog.Infof("Added route: %s via %s", r, ni.devName())66 return nil67}68func (ni *networkInterface) routeDel(ipDst cidrIPDst) error {69 r := ipDst.string()70 if len(r) == 0 {71 return nil72 }73 ifcLink, err := netlink.LinkByName(ni.devName())74 if err != nil {75 return errors.Wrapf(err, "[%v] function netlink.LinkByName()", errors.Trace())76 }77 _, dst, err := net.ParseCIDR(r)78 if err != nil {79 return errors.Wrapf(err, "[%v] function net.ParseCIDR()", errors.Trace())80 }81 route := &netlink.Route{82 LinkIndex: ifcLink.Attrs().Index,83 Dst: dst,84 }85 sysRouteList, err := netlink.RouteList(ifcLink, netlink.FAMILY_ALL)86 if err != nil {87 return errors.Wrapf(err, "[%v] function netlink.RouteGet()", errors.Trace())88 }89 for _, sysRoute := range sysRouteList {90 if route.Dst.String() == sysRoute.Dst.String() && route.LinkIndex == sysRoute.LinkIndex {91 if err := netlink.RouteDel(route); err != nil {92 return errors.Wrapf(err, "[%v] function netlink.RouteDel()", errors.Trace())93 }...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run K6 automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful