使用 go-lang 将网络共享映射到 Windows 驱动器的最佳方法是什么?此共享还需要用户名和密码。 对 python What is the best way to map windows drives using Python? 提出了类似的问题
最佳答案
到目前为止,在 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))
}
关于windows - 使用 golang 映射 Windows 驱动器的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41277465/