Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package device
- import (
- "log"
- "os/exec"
- "strings"
- )
- // Returns an array of IPs from a device
- func RetrieveIPs() []string {
- result, err := executeCmdToRetrieveIPs()
- if err != nil {
- log.Fatalln(err)
- }
- return arrayStoreIPs(result)
- }
- // Executes a command in powershell that retrieves outgoing traffics
- func executeCmdToRetrieveIPs() ([]byte, error) {
- log.Println("executeCmdToRetrieveIPs function executed")
- cmd := exec.Command("powershell", "-Command", "Get-NetTCPConnection -State Established -AppliedSetting Internet | Select-Object RemoteAddress | Sort-Object RemoteAddress -Unique | Format-Table -HideTableHeaders")
- result, err := cmd.CombinedOutput()
- return result, err
- }
- // Parses the output of the executed command to retrieve outgoing traffics from a device and stores it in an array
- func arrayStoreIPs(result []byte) []string {
- resultString := string(result)
- arrayOfIPs := strings.Split(resultString, "\n")
- ipAddresses := []string{}
- for _, line := range arrayOfIPs {
- ipAddress := strings.TrimSpace(line)
- // Checks if the trimmed line is empty
- if ipAddress != "" {
- ipAddresses = append(ipAddresses, ipAddress)
- }
- }
- return ipAddresses
- }
Advertisement
Add Comment
Please, Sign In to add comment