1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
|
smb2
====
[](https://github.com/cloudsoda/go-smb2/actions/workflows/go.yml)
[](https://pkg.go.dev/github.com/cloudsoda/go-smb2)
Description
-----------
An SMB2/3 client implementation. This is a fork of the project [github.com/hirochachacha/go-smb2](https://github.com/hirochachacha/go-smb2). Any releases will be pre-1.0.0 for some time as features and bug fixes are implemented.
Installation
------------
`go get github.com/cloudsoda/go-smb2`
Documentation
-------------
https://pkg.go.dev/github.com/cloudsoda/go-smb2
Examples
--------
### List share names ###
```go
package main
import (
"fmt"
"net"
"github.com/cloudsoda/go-smb2"
)
func main() {
d := &smb2.Dialer{
Initiator: &smb2.NTLMInitiator{
User: "USERNAME",
Password: "PASSWORD",
},
}
s, err := d.Dial(context.Background(), "SERVERNAME:445")
if err != nil {
panic(err)
}
defer s.Logoff()
names, err := s.ListSharenames()
if err != nil {
panic(err)
}
for _, name := range names {
fmt.Println(name)
}
}
```
### File manipulation ###
```go
package main
import (
"io"
"net"
"github.com/cloudsoda/go-smb2"
)
func main() {
d := &smb2.Dialer{
Initiator: &smb2.NTLMInitiator{
User: "USERNAME",
Password: "PASSWORD",
},
}
s, err := d.Dial(context.Background(), "SERVERNAME:445")
if err != nil {
panic(err)
}
defer s.Logoff()
fs, err := s.Mount("SHARENAME")
if err != nil {
panic(err)
}
defer fs.Umount()
f, err := fs.Create("hello.txt")
if err != nil {
panic(err)
}
defer fs.Remove("hello.txt")
defer f.Close()
_, err = f.Write([]byte("Hello world!"))
if err != nil {
panic(err)
}
_, err = f.Seek(0, io.SeekStart)
if err != nil {
panic(err)
}
bs, err := io.ReadAll(f)
if err != nil {
panic(err)
}
fmt.Println(string(bs))
}
```
### Check error types ###
```go
package main
import (
"context"
"fmt"
"net"
"os"
"github.com/cloudsoda/go-smb2"
)
func main() {
d := &smb2.Dialer{
Initiator: &smb2.NTLMInitiator{
User: "USERNAME",
Password: "PASSWORD",
},
}
s, err := d.Dial(context.Background(), "SERVERNAME:445")
if err != nil {
panic(err)
}
defer s.Logoff()
fs, err := s.Mount("SHARENAME")
if err != nil {
panic(err)
}
defer fs.Umount()
_, err = fs.Open("notExist.txt")
fmt.Println(os.IsNotExist(err)) // true
fmt.Println(os.IsExist(err)) // false
fs.WriteFile("hello2.txt", []byte("test"), 0444)
err = fs.WriteFile("hello2.txt", []byte("test2"), 0444)
fmt.Println(os.IsPermission(err)) // true
ctx, cancel := context.WithTimeout(context.Background(), 0)
defer cancel()
_, err = fs.WithContext(ctx).Open("hello.txt")
fmt.Println(os.IsTimeout(err)) // true
}
```
### Glob and WalkDir through FS interface ###
```go
package main
import (
"fmt"
"net"
iofs "io/fs"
"github.com/cloudsoda/go-smb2"
)
func main() {
d := &smb2.Dialer{
Initiator: &smb2.NTLMInitiator{
User: "USERNAME",
Password: "PASSWORD",
},
}
s, err := d.Dial(context.Background(), "SERVERNAME:445")
if err != nil {
panic(err)
}
defer s.Logoff()
fs, err := s.Mount("SHARENAME")
if err != nil {
panic(err)
}
defer fs.Umount()
matches, err := iofs.Glob(fs.DirFS("."), "*")
if err != nil {
panic(err)
}
for _, match := range matches {
fmt.Println(match)
}
err = iofs.WalkDir(fs.DirFS("."), ".", func(path string, d iofs.DirEntry, err error) error {
fmt.Println(path, d, err)
return nil
})
if err != nil {
panic(err)
}
}
```
|