Guest User

Untitled

a guest
Aug 14th, 2024
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. package device
  2.  
  3. import (
  4. "log"
  5. "os/exec"
  6. "strings"
  7. )
  8.  
  9. // Returns an array of IPs from a device
  10. func RetrieveIPs() []string {
  11. result, err := executeCmdToRetrieveIPs()
  12. if err != nil {
  13. log.Fatalln(err)
  14. }
  15. return arrayStoreIPs(result)
  16. }
  17.  
  18. // Executes a command in powershell that retrieves outgoing traffics
  19. func executeCmdToRetrieveIPs() ([]byte, error) {
  20. log.Println("executeCmdToRetrieveIPs function executed")
  21. cmd := exec.Command("powershell", "-Command", "Get-NetTCPConnection -State Established -AppliedSetting Internet | Select-Object RemoteAddress | Sort-Object RemoteAddress -Unique | Format-Table -HideTableHeaders")
  22. result, err := cmd.CombinedOutput()
  23. return result, err
  24. }
  25.  
  26. // Parses the output of the executed command to retrieve outgoing traffics from a device and stores it in an array
  27. func arrayStoreIPs(result []byte) []string {
  28. resultString := string(result)
  29. arrayOfIPs := strings.Split(resultString, "\n")
  30. ipAddresses := []string{}
  31. for _, line := range arrayOfIPs {
  32. ipAddress := strings.TrimSpace(line)
  33. // Checks if the trimmed line is empty
  34. if ipAddress != "" {
  35. ipAddresses = append(ipAddresses, ipAddress)
  36. }
  37. }
  38. return ipAddresses
  39. }
Advertisement
Add Comment
Please, Sign In to add comment