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

使用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 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示