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

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

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中运行你的代码。

package main

import (
	"bufio"
	"fmt"
	"os"
	"os/exec"
	"strings"
)

type UserGroups map[string][]string

func main() {
	file, err := os.Open("/etc/passwd")
	if err != nil {
		panic(err)
	}
	defer file.Close()

	scanner := bufio.NewScanner(file)

	userGroups := make(UserGroups)

	for scanner.Scan() {
		fields := strings.Split(scanner.Text(), ":")

		username := fields[0]

		cmd := exec.Command("id", "-Gn", username)
		output, err := cmd.Output()
		if err != nil {
			panic(err)
		}

		groups := strings.Split(strings.TrimSpace(string(output)), " ")

		userGroups[username] = groups
	}

	for username, groups := range userGroups {
		fmt.Printf("%s: %s\n", username, strings.Join(groups, ", "))
	}
}

Playground输出:

operator: operator
mail: mail
www-data: www-data
bin: bin
sys: sys
sync: users
nobody: nobody
root: root, wheel
daemon: daemon

程序已退出。
英文:

> 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:

package main

import (
	"bufio"
	"fmt"
	"os"
	"os/exec"
	"strings"
)

type UserGroups map[string][]string

func main() {
	file, err := os.Open("/etc/passwd")
	if err != nil {
		panic(err)
	}
	defer file.Close()

	scanner := bufio.NewScanner(file)

	userGroups := make(UserGroups)

	for scanner.Scan() {
		fields := strings.Split(scanner.Text(), ":")

		username := fields[0]

		cmd := exec.Command("id", "-Gn", username)
		output, err := cmd.Output()
		if err != nil {
			panic(err)
		}

		groups := strings.Split(strings.TrimSpace(string(output)), " ")

		userGroups[username] = groups
	}

	for username, groups := range userGroups {
		fmt.Printf("%s: %s\n", username, strings.Join(groups, ", "))
	}
}

Playground output:

operator: operator
mail: mail
www-data: www-data
bin: bin
sys: sys
sync: users
nobody: nobody
root: root, wheel
daemon: daemon
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:

确定