以管理员身份运行Go程序

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

Running Go Program as Admin

问题

我正在尝试在Windows中以管理员权限运行一个Go程序,类似于Linux中的sudo。为了做到这一点,我正在以管理员身份启动cmd。我还尝试了runas管理员命令。

问题是os.Getuid()函数返回-1。程序会检查它,如果不是0,它会告诉你没有管理员权限。

来自Go程序Gor(listener.go)的代码片段:

if os.Getuid() != 0 {
	fmt.Println("请以root或sudo身份启动监听器!")
	fmt.Println("这是因为监听器需要嗅探给定端口上的流量。")
	os.Exit(1)
}

有没有解决这个问题的办法?

谢谢

英文:

I'm trying to run a Go program with admin rights in Windows. Like sudo in Linux. To do that I'm launching cmd as Administrator. I also tried runas administrator command.

The problem is os.Getuid() function returns -1. The program checks it and if it is not 0 it tells, you don't have the admin rights.

Code snippet from the Go program Gor (listener.go):

if os.Getuid() != 0 {
	fmt.Println("Please start the listener as root or sudo!")
	fmt.Println("This is required since listener sniff traffic on given port.")
	os.Exit(1)
}

Is there any solution for this issue?

Thanks

答案1

得分: 4

Go的旗舰版本是由Go的主要作者为Linux/Darwin开发的,其架构允许适用于其他操作系统。后来,其他人将Go移植到了其他操作系统,如Windows。其中一些Windows移植存在缺陷和不完整的问题。此外,一些Linux/Darwin的特性,如安全模型,在Windows中没有直接的对应物。

syscall_windows.go:

. . .
// TODO(brainman): fix all needed for os
. . .
func Getuid() (uid int) { return -1 }
. . .

在Go的问题跟踪器上开启一个新的问题。

英文:

The flagship version of Go was developed by the principal Go authors for Linux/Darwin, with an architecture that allowed for other operating systems. Later, others came along and ported Go to other operating systems like Windows. Some of the Windows port is flawed and incomplete. Also, some Linux/Darwin features, like the security model, don't have a direct analogue in Windows.

syscall_windows.go:

. . .
// TODO(brainman): fix all needed for os
. . .
func Getuid() (uid int) { return -1 }
. . .

Open a new issue on the Go issue tracker.

huangapple
  • 本文由 发表于 2013年7月10日 20:01:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/17570147.html
匿名

发表评论

匿名网友

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

确定