douke1942 2016-12-22 07:01
浏览 258
已采纳

使用golang映射Windows驱动器的最佳方法是什么?

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?

  • 写回答

1条回答 默认 最新

  • dongnong3799 2017-06-20 08:54
    关注

    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))
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?