package main import ( "fmt" "log" "os/exec" "regexp" ) func main() { ww, err := exec.Command("CMD", "/C", " ping 126.com").Output() if err != nil { log.Fatal(err.Error()) } fmt.Println(string(ww)) ww, err = exec.Command("CMD", "/C", " ipconfig").Output() if err != nil { log.Fatal(err.Error()) } // fmt.Println(string(ww)) reg := regexp.MustCompile(`d+.d+.d+.d+`) fmt.Printf("%q ", reg.FindAllString(string(ww), -1)[0]) }
实例2:
package main import ( "fmt" "net" "strings" ) func main() { conn, err := net.Dial("udp", "google.com:80") if err != nil { fmt.Println(err.Error()) return } defer conn.Close() fmt.Println(strings.Split(conn.LocalAddr().String(), ":")[0]) }