GO: 连接到Unix系统,获取用户的/etc/passwd文件,并找出每个用户所关联的组。

huangapple go评论149阅读模式
英文:

GO : ssh into Unix get users /etc/passwd and finding groups associated for every user

问题

我是新手学习Golang。我能够从cat /etc/passwd获取用户列表。我想要实现的是,对于cat /etc/passwd返回的每个用户,我想要运行"id -user"命令,并将用户组映射到该用户。我应该使用什么数据结构?

还是有一个Shell命令可以实现上述目标吗?
我尝试了这个命令:

for user in $(awk -F: '{print $1}' /etc/passwd); do groups $user; done

tail -n +1 -F /etc/passwd | head -n +2500 -f":" /etc/passwd

英文:

I am new to Golang. I am able to get the list of users from cat /etc/passwd. What I am trying to achieve is that for every user returned by cat /etc/passwd, I want to run the "id -user" command and map the user groups to that user. What data structure should I use? OR

Is there a Shell command that I can get fire to achieve the above?
I tried this -

for user in $(awk -F: '{print $1}' /etc/passwd); do groups $user; done

AND

tail -n +1 -F /etc/passwd | head -n +2500 -f":" /etc/passwd

答案1

得分: 1

你可以使用各种数据结构,包括数组、列表、映射、字典等,对于数据结构的选择没有具体的限制。这里提供了一个使用map数据结构和string的示例代码。关于shell命令,os/exec包提供了你在这里需要的功能(exec.Command)。在Playground中运行你的代码。

  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "os"
  6. "os/exec"
  7. "strings"
  8. )
  9. type UserGroups map[string][]string
  10. func main() {
  11. file, err := os.Open("/etc/passwd")
  12. if err != nil {
  13. panic(err)
  14. }
  15. defer file.Close()
  16. scanner := bufio.NewScanner(file)
  17. userGroups := make(UserGroups)
  18. for scanner.Scan() {
  19. fields := strings.Split(scanner.Text(), ":")
  20. username := fields[0]
  21. cmd := exec.Command("id", "-Gn", username)
  22. output, err := cmd.Output()
  23. if err != nil {
  24. panic(err)
  25. }
  26. groups := strings.Split(strings.TrimSpace(string(output)), " ")
  27. userGroups[username] = groups
  28. }
  29. for username, groups := range userGroups {
  30. fmt.Printf("%s: %s\n", username, strings.Join(groups, ", "))
  31. }
  32. }

Playground输出:

  1. operator: operator
  2. mail: mail
  3. www-data: www-data
  4. bin: bin
  5. sys: sys
  6. sync: users
  7. nobody: nobody
  8. root: root, wheel
  9. daemon: daemon
  10. 程序已退出。
英文:

> What data structure should I use?

You could use anything from arrays, lists, maps, dictionaries, to anything, there are no specific constraints here for data structure selection. Here, code provided an example with a map data structure with mixups of string. Regarding shell command, the os/exec package provides functionality that you are needing here (exec.Command). Run your code in Playground:

  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "os"
  6. "os/exec"
  7. "strings"
  8. )
  9. type UserGroups map[string][]string
  10. func main() {
  11. file, err := os.Open("/etc/passwd")
  12. if err != nil {
  13. panic(err)
  14. }
  15. defer file.Close()
  16. scanner := bufio.NewScanner(file)
  17. userGroups := make(UserGroups)
  18. for scanner.Scan() {
  19. fields := strings.Split(scanner.Text(), ":")
  20. username := fields[0]
  21. cmd := exec.Command("id", "-Gn", username)
  22. output, err := cmd.Output()
  23. if err != nil {
  24. panic(err)
  25. }
  26. groups := strings.Split(strings.TrimSpace(string(output)), " ")
  27. userGroups[username] = groups
  28. }
  29. for username, groups := range userGroups {
  30. fmt.Printf("%s: %s\n", username, strings.Join(groups, ", "))
  31. }
  32. }

Playground output:

  1. operator: operator
  2. mail: mail
  3. www-data: www-data
  4. bin: bin
  5. sys: sys
  6. sync: users
  7. nobody: nobody
  8. root: root, wheel
  9. daemon: daemon
  10. Program exited.

huangapple
  • 本文由 发表于 2023年5月6日 13:32:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76187312.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定