【问题标题】:What is the best way to map windows drives using golang?使用 golang 映射 Windows 驱动器的最佳方法是什么?
【发布时间】:2016-12-22 07:01:47
【问题描述】:

使用 go-lang 将网络共享映射到 Windows 驱动器的最佳方法是什么?此共享还需要用户名和密码。 类似的问题被问到 python What is the best way to map windows drives using Python?

【问题讨论】:

  • @Nadh:问题是如何映射网络驱动器,而不是如何列出(网络)驱动器。
  • 啊,是的,我的错。删除了标志。

标签: windows go mapping drive


【解决方案1】:

到目前为止,在 Go 中还没有直接的方法可以做到这一点;我建议使用net use,这当然会将功能限制在 Windows 中,但这实际上是您所需要的。

因此,当您在 Windows 中打开命令提示符时,您可以使用以下方法将网络共享映射到 Windows 驱动器:

net use Q: \\SERVER\SHARE /user:Alice pa$$word /P

Q: 代表您的 Windows 驱动器,\\SERVER\SHARE 是网络地址,/user:Alice pa$$word 是您的凭据,/P 是持久性。

在 Go 中执行这个看起来像:

func mapDrive(letter string, address string, user string, pw string) ([]byte, error) {
  // return combined output for std and err
  return exec.Command("net use", letter, address, fmt.Sprintf("/user:%s", user), pw, "/P").CombinedOutput()
}

func main() {
  out, err := mapDrive("Q:", `\\SERVER\SHARE`, "Alice", "pa$$word")
  if err != nil {
    log.Fatal(err)
  }
  // print whatever comes out
  log.Println(string(out))
}

【讨论】:

    猜你喜欢
    • 2010-11-19
    • 1970-01-01
    • 2010-10-21
    • 1970-01-01
    • 1970-01-01
    • 2011-10-14
    • 2018-03-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多