github.com/wtfutil/wtf@v0.43.0/modules/security/dns.go (about)

     1  package security
     2  
     3  import (
     4  	"os/exec"
     5  	"runtime"
     6  	"strings"
     7  
     8  	"github.com/wtfutil/wtf/utils"
     9  )
    10  
    11  /* -------------------- Exported Functions -------------------- */
    12  
    13  func DnsServers() []string {
    14  	switch runtime.GOOS {
    15  	case "linux":
    16  		return dnsLinux()
    17  	case "darwin":
    18  		return dnsMacOS()
    19  	case "windows":
    20  		return dnsWindows()
    21  	default:
    22  		return []string{runtime.GOOS}
    23  	}
    24  }
    25  
    26  /* -------------------- Unexported Functions -------------------- */
    27  
    28  func dnsLinux() []string {
    29  	// This may be very Ubuntu specific
    30  	cmd := exec.Command("nmcli", "device", "show")
    31  	out := utils.ExecuteCommand(cmd)
    32  
    33  	lines := strings.Split(out, "\n")
    34  
    35  	dns := []string{}
    36  
    37  	for _, l := range lines {
    38  		if strings.HasPrefix(l, "IP4.DNS") {
    39  			parts := strings.Split(l, ":")
    40  			dns = append(dns, strings.TrimSpace(parts[1]))
    41  		}
    42  	}
    43  	return dns
    44  }
    45  
    46  func dnsMacOS() []string {
    47  	cmdString := `scutil --dns | head -n 7 | grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}'`
    48  	cmd := exec.Command("sh", "-c", cmdString)
    49  	out := utils.ExecuteCommand(cmd)
    50  
    51  	lines := strings.Split(out, "\n")
    52  
    53  	if len(lines) > 0 {
    54  		return lines
    55  	}
    56  
    57  	return []string{}
    58  }
    59  
    60  func dnsWindows() []string {
    61  
    62  	cmd := exec.Command("powershell.exe", "-NoProfile", "Get-DnsClientServerAddress | Select-Object –ExpandProperty ServerAddresses")
    63  
    64  	return []string{utils.ExecuteCommand(cmd)}
    65  }