Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/hirochachacha/go-smb2
SMB2/3 client library written in Go.
https://github.com/hirochachacha/go-smb2
go golang smb smb2
Last synced: 22 days ago
JSON representation
SMB2/3 client library written in Go.
- Host: GitHub
- URL: https://github.com/hirochachacha/go-smb2
- Owner: hirochachacha
- License: bsd-2-clause
- Created: 2016-07-17T07:57:33.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2023-09-28T09:09:36.000Z (about 1 year ago)
- Last Synced: 2024-08-05T17:25:14.342Z (4 months ago)
- Topics: go, golang, smb, smb2
- Language: Go
- Homepage:
- Size: 290 KB
- Stars: 344
- Watchers: 14
- Forks: 93
- Open Issues: 35
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-hacking-lists - hirochachacha/go-smb2 - SMB2/3 client library written in Go. (Go)
README
smb2
====[![Build Status](https://github.com/hirochachacha/go-smb2/actions/workflows/go.yml/badge.svg)](https://github.com/hirochachacha/go-smb2/actions/workflows/go.yml)
[![Go Reference](https://pkg.go.dev/badge/github.com/hirochachacha/go-smb2.svg)](https://pkg.go.dev/github.com/hirochachacha/go-smb2)Description
-----------SMB2/3 client implementation.
Installation
------------`go get github.com/hirochachacha/go-smb2`
Documentation
-------------http://godoc.org/github.com/hirochachacha/go-smb2
Examples
--------### List share names ###
```go
package mainimport (
"fmt"
"net""github.com/hirochachacha/go-smb2"
)func main() {
conn, err := net.Dial("tcp", "SERVERNAME:445")
if err != nil {
panic(err)
}
defer conn.Close()d := &smb2.Dialer{
Initiator: &smb2.NTLMInitiator{
User: "USERNAME",
Password: "PASSWORD",
},
}s, err := d.Dial(conn)
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 mainimport (
"io"
"io/ioutil"
"net""github.com/hirochachacha/go-smb2"
)func main() {
conn, err := net.Dial("tcp", "SERVERNAME:445")
if err != nil {
panic(err)
}
defer conn.Close()d := &smb2.Dialer{
Initiator: &smb2.NTLMInitiator{
User: "USERNAME",
Password: "PASSWORD",
},
}s, err := d.Dial(conn)
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 := ioutil.ReadAll(f)
if err != nil {
panic(err)
}fmt.Println(string(bs))
}
```### Check error types ###
```go
package mainimport (
"context"
"fmt"
"net"
"os""github.com/hirochachacha/go-smb2"
)func main() {
conn, err := net.Dial("tcp", "SERVERNAME:445")
if err != nil {
panic(err)
}
defer conn.Close()d := &smb2.Dialer{
Initiator: &smb2.NTLMInitiator{
User: "USERNAME",
Password: "PASSWORD",
},
}s, err := d.Dial(conn)
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)) // falsefs.WriteFile("hello2.txt", []byte("test"), 0444)
err = fs.WriteFile("hello2.txt", []byte("test2"), 0444)
fmt.Println(os.IsPermission(err)) // truectx, 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 mainimport (
"fmt"
"net"
iofs "io/fs""github.com/hirochachacha/go-smb2"
)func main() {
conn, err := net.Dial("tcp", "SERVERNAME:445")
if err != nil {
panic(err)
}
defer conn.Close()d := &smb2.Dialer{
Initiator: &smb2.NTLMInitiator{
User: "USERNAME",
Password: "PASSWORD",
},
}s, err := d.Dial(conn)
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)
}
}
```