如何在Windows上使用Golang执行netsh命令

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

How to execute netsh command from Golang on Windows

问题

如何在 Golang 中执行需要“以管理员身份运行”的 netsh 命令?

err1 := exec.Command("netsh", "interface ipv6 set privacy state=disable").Run()
fmt.Printf("Exec1 err: %+v\n", err1)

要在 Golang 中执行需要以管理员身份运行的 netsh 命令,你可以使用 exec.Command 函数来创建一个命令对象,并指定要执行的命令和参数。然后,使用命令对象的 Run 方法来执行该命令。

请注意,执行需要管理员权限的命令时,你的程序本身也需要以管理员身份运行。否则,命令将无法成功执行。

英文:

How to execute
> netsh

command from Golang which requires "run as Admin"?

err1 := exec.Command("netsh", "interface ipv6 set privacy state=disable").Run()
fmt.Printf("Exec1 err: %+v\n", err1)

答案1

得分: 1

尝试执行以下命令:exec.Command("netsh", "interface", "ipv6", "set", "privacy", "state=disable").Run()

英文:

Try exec.Command("netsh", "interface", "ipv6", "set", "privacy", "state=disable").Run()

huangapple
  • 本文由 发表于 2017年2月13日 20:56:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/42204608.html
匿名

发表评论

匿名网友

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

确定