Skip to content

Instantly share code, notes, and snippets.

@srid
Created October 4, 2013 01:52
Show Gist options
  • Save srid/6819841 to your computer and use it in GitHub Desktop.
Save srid/6819841 to your computer and use it in GitHub Desktop.
// GetNameServers returns nameservers configured on the host.
func GetNameServers() ([]string, error) {
resolvFile := "/etc/resolv.conf"
f, err := os.Open(resolvFile)
if err != nil {
return nil, err
}
defer f.Close()
addrs := []string{}
scanner := bufio.NewScanner(f)
for scanner.Scan() {
line := scanner.Text()
parts := strings.SplitN(line, " ", 2)
if len(parts) == 2 && parts[0] == "nameserver" {
addrs = append(addrs, parts[1])
}
}
if err = scanner.Err(); err != nil {
return nil, err
}
return addrs, err
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment