Os执行Sudo命令在Go中 [英] Os Exec Sudo Command in Go

查看:106
本文介绍了Os执行Sudo命令在Go中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在熟悉Go和goroutines的过程中,我碰到了执行命令的障碍。这些命令的格式如下:

  sudo find / folder -type f |同时阅读我;做sudo -S chmod 644$ i;完成

使用如何在未知参数中执行Golang中的系统命令我试图执行这个命令,但我认为它没有执行由于第一个参数是sudo,我可能是错的。我只有两个问题。



当这些命令无法运行时,我返回退出状态1,是否有办法获得比我所做的更详细的错误?问题二,为什么我会用这个脚本获得退出状态1?这是不是应该怎么办?

 包主

导入(
bufio
fmt
os
os / exec
字符串
同步


func ExeCmd(cmd string,wg * sync.WaitGroup){
parts:= strings.Fields(cmd)
head:= parts [0]
//在此处点是sudo
parts = parts [1:len(parts)]
out,err:= exec.Command(head,parts ...)。Output()
if err != nil {
fmt.Printf(%s \ n,err)
}
fmt.Printf(%s\\\
,out)
wg .Done()//信号到WaitGroup goroutine完成
}

func InArray(a []字符串,e字符串)bool {
for _,x:= range a {
if x == e {
return true
fmt.Print(True)
}
}
return false
}

func main(){
exec.Command(sudo ls> / dev / null)//获得sudo密码一次,似乎不会o使用Go
wg:= new(sync.WaitGroup)
reader:= bufio.NewReader(os.Stdin)
fdbslices:= [] string {f,d ,b,files,directories,both}
commands:= [] string {}
fdb:=
filep:=
dirp:=

//抓取路径
fmt.Print(Path:)
findpath,_:= reader.ReadString('\\\
')
findpath = strings.ToLower(strings.Trim(findpath,\\\
))

//抓取文件,目录或两者
for {
fmt.Print(Files,Directories,or Both:)
fdb,_ = reader.ReadString('\\\
')
fdb = strings.ToLower(strings.Trim(fdb,\\ \\ n))
如果InArray(fdbslices,fdb)== true {break}
}

//构建命令字符串,它们应该是
为{
如果fdb ==f|| fdb ==files{
fmt.Print(Permissions for Files:)
filep,_ = reader.ReadString('\\\
')
filep = strings.Trim( filep,\\\

commands = append(commands,sudo find+ findpath +-type f | while read i; do sudo -S chmod+ filep +\$ i \\ \\;完成)
}
if fdb ==d|| fdb ==目录{
fmt.Print(目录权限:)
dirp,_ = reader.ReadString('\\\
')
dirp = strings.Trim( dirp,\\\

commands = append(命令,sudo find+ findpath +-type d | while read i; do sudo -S chmod+ dirp +\$ i \\ \\;完成)
}
if fdb ==b|| fdb ==both{
fmt.Print(Permissions for Files:)
filep,_ = reader.ReadString('\\\
')
filep = strings.Trim( filep,\\\

commands = append(commands,sudo find+ findpath +-type f | while read i; do sudo -S chmod+ filep +\$ i \\ \\;完成)
fmt.Print(目录权限:)
dirp,_ = reader.ReadString('\\\
')
dirp = strings.Trim(dirp ,\\\

命令= append(命令,sudo find+ findpath +-type d | while read i; do sudo -S chmod+ dirp +\$ i \ ; done)
}
break
}

//执行命令
用于_,str:=范围命令{
wg .Add(1)
fmt.Print(Doing:+ str +\r\\\

go ExeCmd(str,wg)
}

wg.Wait()
}

终端输出示例:

 路径:test 
文件,目录或两者:b
文件权限:644
目录权限:755
执行:find test -type f |同时阅读我;做sudo -S chmod 644$ i;完成
操作:找到test -type d |同时阅读我;做sudo -S chmod 755$ i;完成
退出状态1

退出状态1

谢谢为你的时间。

解决方案

exec.Command()内核直接执行给定的进程。然而,你传递的命令是一个由shell脚本连接在一起的多个程序串。



如果你想执行一个shell脚本,你应该使用类似这样的东西:

  cmd:= exec.Command(/ bin / sh,-c,sudo find ... )


In the process of familiarizing myself with Go and goroutines I have hit a road block with executing commands. The format of these commands are:

sudo find /folder -type f | while read i; do sudo -S chmod 644 "$i"; done

With code taken from How to execute system command in Golang with unknown arguments I'm trying to execute this command but I believe it's not executing due to the first argument being sudo, I could be wrong. I have just two questions.

When these commands fail to run I am returned "exit status 1", is there a way to get a more detailed error than what I am doing? Question two, why would I be getting "exit status 1" with this script? What is going on that isn't supposed to?

package main

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

func ExeCmd(cmd string, wg *sync.WaitGroup) {
    parts := strings.Fields(cmd)
    head := parts[0]
    // Head at this point is "sudo"
    parts = parts[1:len(parts)]
    out, err := exec.Command(head,parts...).Output()
    if err != nil {
        fmt.Printf("%s\n", err)
    }
    fmt.Printf("%s\n", out)
    wg.Done() // Signal to WaitGroup goroutine finished
}

func InArray(a []string, e string) bool {
    for _, x := range a {
        if x == e {
            return true
            fmt.Print("True")
        }
    }
    return false
}

func main() {
    exec.Command("sudo ls > /dev/null") // Get sudo password just once, doesn't seem to work with Go
    wg := new(sync.WaitGroup)
    reader := bufio.NewReader(os.Stdin)
    fdbslices := []string{"f", "d", "b", "files", "directories", "both"}
    commands := []string{}
    fdb := ""
    filep := ""
    dirp := ""

    // Grab Path
    fmt.Print("Path: ")
    findpath, _ := reader.ReadString('\n')
    findpath = strings.ToLower(strings.Trim(findpath, "\n"))

    // Grab Files, Directories, or Both
    for {
        fmt.Print("Files, Directories, or Both: ")
        fdb, _ = reader.ReadString('\n')
        fdb = strings.ToLower(strings.Trim(fdb, "\n"))
        if InArray(fdbslices, fdb) == true { break }
    }

    // Build commands string, these come out as they should
    for {
        if fdb == "f" || fdb == "files" {
            fmt.Print("Permissions for Files: ")
            filep, _ = reader.ReadString('\n')
            filep = strings.Trim(filep, "\n")
            commands = append(commands, "sudo find " + findpath + " -type f | while read i; do sudo -S chmod " + filep + " \"$i\"; done")
        }
        if fdb == "d" || fdb == "directories" {
            fmt.Print("Permissions for Directories: ")
            dirp, _ = reader.ReadString('\n')
            dirp = strings.Trim(dirp, "\n")
            commands = append(commands, "sudo find " + findpath + " -type d | while read i; do sudo -S chmod " + dirp + " \"$i\"; done")
        }
        if fdb == "b" || fdb == "both" {
            fmt.Print("Permissions for Files: ")
            filep, _ = reader.ReadString('\n')
            filep = strings.Trim(filep, "\n")
            commands = append(commands, "sudo find " + findpath + " -type f | while read i; do sudo -S chmod " + filep + " \"$i\"; done")
            fmt.Print("Permissions for Directories: ")
            dirp, _ = reader.ReadString('\n')
            dirp = strings.Trim(dirp, "\n")
            commands = append(commands, "sudo find " + findpath + " -type d | while read i; do sudo -S chmod " + dirp + " \"$i\"; done")
        }
        break
    }

    // Execute Commands
    for _, str := range commands {
        wg.Add(1)
        fmt.Print("Doing: " + str + "\r\n")
        go ExeCmd(str, wg)
    }

    wg.Wait()
}

Example Terminal output:

Path: test
Files, Directories, or Both: b
Permissions for Files: 644
Permissions for Directories: 755
Doing: find test -type f | while read i; do sudo -S chmod 644 "$i"; done
Doing: find test -type d | while read i; do sudo -S chmod 755 "$i"; done
exit status 1

exit status 1

Thank you for your time.

解决方案

exec.Command() will ask the kernel to execute the given process directly. However, the command you are passing is a string of multiple programs hooked together by a shell script.

If you want to execute a shell script, you should use something like this:

cmd := exec.Command("/bin/sh", "-c", "sudo find ...")

这篇关于Os执行Sudo命令在Go中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆