github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/helper/freeport/ephemeral_linux.go (about) 1 //+build linux 2 3 package freeport 4 5 import ( 6 "fmt" 7 "os/exec" 8 "regexp" 9 "strconv" 10 ) 11 12 /* 13 $ sysctl -n net.ipv4.ip_local_port_range 14 32768 60999 15 */ 16 17 const ephemeralPortRangeSysctlKey = "net.ipv4.ip_local_port_range" 18 19 var ephemeralPortRangePatt = regexp.MustCompile(`^\s*(\d+)\s+(\d+)\s*$`) 20 21 func getEphemeralPortRange() (int, int, error) { 22 cmd := exec.Command("sysctl", "-n", ephemeralPortRangeSysctlKey) 23 out, err := cmd.Output() 24 if err != nil { 25 return 0, 0, err 26 } 27 28 val := string(out) 29 30 m := ephemeralPortRangePatt.FindStringSubmatch(val) 31 if m != nil { 32 min, err1 := strconv.Atoi(m[1]) 33 max, err2 := strconv.Atoi(m[2]) 34 35 if err1 == nil && err2 == nil { 36 return min, max, nil 37 } 38 } 39 40 return 0, 0, fmt.Errorf("unexpected sysctl value %q for key %q", val, ephemeralPortRangeSysctlKey) 41 }