Is it possible to run a sudo command in go without running the program itsself as sudo

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

Is it possible to run a sudo command in go without running the program itsself as sudo

问题

该程序运行多个需要sudo权限的命令(例如sudo dnf update)。由于该程序应该使用go install命令安装,因此无法在没有用户配置的情况下作为sudo自身运行(据我所知)。

该程序不会向用户显示输出,以保持输出的清晰。为了显示进程正在运行,它使用了来自spinner库的旋转器。

是否可能执行以下任何操作?

  • 从程序内部获取sudo权限
  • 使程序可在使用go install安装后以sudo方式运行
  • 显示sudo命令的输出(包括密码请求),而不被旋转器覆盖

这是我想要我的代码做的简化版本:

package main

import (
	"fmt"
	"os"
	"os/exec"
	"time"

	"github.com/briandowns/spinner"
)

func main() {
	// 用于显示正在运行的旋转器
	s := spinner.New(spinner.CharSets[14], time.Millisecond*100)
	s.Start()

	// 以某种方式使用sudo执行此命令
	_, err := exec.Command(os.Getenv("SHELL"), "-c", "dnf update -y").Output()

	// 停止旋转器并处理任何错误
	if err != nil {
		fmt.Printf("Err: %s", err)
		os.Exit(1)
	}
	s.Stop()

	// 完成
	fmt.Println("Success")
}
英文:

The program runs multiple commands that require sudo privileges (such as sudo dnf update). Since the program should be installed using the go install command, it can't be run as sudo its self without configuration done by the user (afaik).

The program doesn't show the output to the user to keep the output clean. To show that a process is running, it uses a spinner from the spinner library.

Is it possible to do any of these things?

  • Obtain sudo privileges from within the program
  • Make the program runnable as sudo, even when installed using go install
  • Show the output of the sudo command (including the password request) without it being overwritten by the spinner

Here is a shortened version of what I would like my code to do:

package main

import (
	"fmt"
	"os"
	"os/exec"
	"time"

	"github.com/briandowns/spinner"
)

func main() {
	// Spinner to show that it's running
	s := spinner.New(spinner.CharSets[14], time.Millisecond*100)
	s.Start()

	// Somehow execute this with sudo
	_, err := exec.Command(os.Getenv("SHELL"), "-c", "dnf update -y").Output()

	// Stop the spinner and handle any error
	if err != nil {
		fmt.Printf("Err: %s", err)
		os.Exit(1)
	}
	s.Stop()

	// Finish
	fmt.Println("Success")
}

答案1

得分: 0

在这个例子中,在你想要以sudo身份运行的命令之前添加sudo,然后运行程序后,会要求输入sudo密码。*如果你将这个应用到你的示例代码中,你可能看不到密码请求消息,因为旋转图形会覆盖它,但如果你不使用旋转图形,你就可以看到它。*即使你看不到这条消息,如果你输入正确的密码并按下回车键,你的命令将作为sudo运行。通过这种方式,你不需要以sudo身份运行你的应用程序。我使用类似的命令运行过,并且它们都起作用了。

英文:

_, err := exec.Command(os.Getenv("SHELL"), "-c", "sudo dnf update -y").Output()

In this exapmle, with adding sudo before the command that you want run as sudo, and after running the program, will ask password for sudo, If you apply this to your example code you can't see password request message, because the spinner graphics will overwrite it, but if you try this without spinner graphics you can see it. Even you don't see the message, if you type your correct password and press enter your commands will work as sudo. With this, you don't need run your application as sudo. I have ran similar commands with using this and they have worked.

huangapple
  • 本文由 发表于 2023年6月1日 22:46:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76383134.html
匿名

发表评论

匿名网友

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

确定