【发布时间】:2020-04-17 19:22:12
【问题描述】:
我正在尝试使用 Go 列出从 Linux 到 Windows 的共享上的目录内容。 到目前为止,我已经设法在具有完整读/写权限的共享中创建/删除新文件。
Go 模块:https://godoc.org/github.com/hirochachacha/go-smb2#Client
功能:
func connect_client(host string, share string, session map[string]string) *smb2.Client {
//Checks for a connection on port
conn, err := net.Dial("tcp", host+":445")
if err != nil {
panic(err)
}
//smb auth
d := &smb2.Dialer{
Initiator: &smb2.NTLMInitiator{
User: session["Username"],
Password: session["Password"],
Domain: session["Domain"],
},
}
//Returns a client session
client, err := d.Dial(conn)
if err != nil {
fmt.Println("Connection failed")
client.Logoff()
} else {
fmt.Println("Connection Succeeded")
}
return client
}
func check_write(host string, client *smb2.Client) {
file := "asdasdas.txt"
fs, err := client.Mount(host)
if err != nil {
fmt.Println(err)
os.Exit(0)
}
defer fs.Umount()
share := strings.Split(host, `\\`)
f, err := fs.Create(file)
if err != nil {
fmt.Println("You do not have write permissions on directory:%s ! \n", strings.Split(share[1], `\`)[1])
os.Exit(0)
}
defer fs.Remove(file)
defer f.Close()
fmt.Printf("You have write permissions to directory: %s \n", strings.Split(share[1], `\`)[1]))
}
func list_all(client *smb2.Client, host string) {
fs, err := client.Mount(host)
if err != nil {
fmt.Println(err)
os.Exit(0)
}
defer fs.Umount()
_, err = fs.Open(`Test.txt`)
if err != nil {
fmt.Println(err)
os.Exit(0)
}
}
func main() {
host, share, action, session := get_flags()
client := connect_client(host, share, session)
full_host := `\\` + host + `\` + share
//File create
if action == "check_write" {
check_write(full_host, client)
}
if action == "list_files" {
list_all(client, full_host)
}
}
在函数 list_all() 中一切正常,但是当我尝试访问 \\192.168.1.19\Sharing 时.. 当我只输入具有目录名称的主机时,它无法列出目录路径,因为它找不到指定的对象。 我不明白如何获取用于 *RemoteFile 的指针以使用函数:
f.Readdir()
f.Name() 等等……
到目前为止,我设法将 *RemoteFileSystem 仅用于所有其他操作,但我想列出目录的所有内容..
我们将不胜感激!
编辑: 如果还不够清楚,请使用以下功能: f.Readdir() f.Name() 我需要获取 *RemoteFile 的指针,这是我的主要问题
【问题讨论】: