英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论