Skip to content

Instantly share code, notes, and snippets.

@yeahnoob
Last active March 30, 2016 02:53
Show Gist options
  • Save yeahnoob/f050acbade6fbd589f7a to your computer and use it in GitHub Desktop.
Save yeahnoob/f050acbade6fbd589f7a to your computer and use it in GitHub Desktop.
retrieve `netstat.exe` command result in WIN32 system
/// Get the STY Water Supply Department's IP address
package main
import (
"bytes"
"fmt"
"log"
"os/exec"
"regexp"
)
func main() {
cmd := exec.Command("c:\\WINDOWS\\system32\\netstat.exe", "-n", "-t")
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
log.Fatal(err)
}
// fmt.Printf("%q\n", out.String())
ipreg := regexp.MustCompile(`\d+\.\d+\.\d+\.\d+`)
localreg := regexp.MustCompile(`192\.168\.1\.\d+`)
tcpip := ipreg.FindAllString(out.String(), -1)
for _, i := range tcpip {
if localreg.MatchString(i) {
fmt.Println(i)
break
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment