使用golang映射Windows驱动器的最佳方式是什么? [英] What is the best way to map windows drives using golang?
问题描述
使用go-lang将网络共享映射到Windows驱动器的最佳方法是什么?此份额还需要用户名和密码。
一个类似的问题被要求用python 使用Python映射Windows驱动器的最佳方式是什么?
到目前为止,在Go中没有直接的方法来做到这一点;我建议使用 net use
,这当然会限制Windows的功能,但这实际上就是您需要的。
因此,当您在Windows中打开命令提示符时,您可以使用以下命令将网络共享映射到Windows驱动器:
net use Q:\\\ \\服务器\ SHARE /用户:Alice pa $$ word / P
Q:
表示您的Windows驱动器, \\SERVER\SHARE
是网络地址, / user:Alice pa $$ word
是你的凭证,而 / P
是持久性的。
如:
func mapDrive(字符串,地址字符串,用户字符串,pw字符串)([] byte,error){
//为std和err返回组合输出
返回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)
}
//打印任意出来
log.Println(字符串(出))
}
What is the best way to map a network share to a windows drive using go-lang? This share also requires a username and password. A similar question was asked for python What is the best way to map windows drives using Python?
As of now there is no direct way to do that in Go; I would recommend using net use
, which of course limits the functionality to Windows, but that's actually what you need.
So, when you open a command prompt in Windows you can map network shares to Windows drives by using:
net use Q: \\SERVER\SHARE /user:Alice pa$$word /P
Q:
represents your windows drive, \\SERVER\SHARE
is the network address, /user:Alice pa$$word
are your credentials, and /P
is for persistence.
Executing this in Go would look something like:
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))
}
这篇关于使用golang映射Windows驱动器的最佳方式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!