Go 实现本地文件上传至服务器,从服务器下载文件 功能

时间:2025-03-03 07:06:59
package main import ( "fmt" "/pkg/sftp" "/x/crypto/ssh" "io" "log" "net" "os" "time" ) //连接的配置 type ClientConfig struct { Host string //ip Port int64 // 端口 Username string //用户名 Password string //密码 sshClient *ssh.Client //ssh client sftpClient *sftp.Client //sftp client LastResult string //最近一次运行的结果 } func (cliConf *ClientConfig) createClient(host string, port int64, username, password string) { var ( sshClient *ssh.Client sftpClient *sftp.Client err error ) cliConf.Host = host cliConf.Port = port cliConf.Username = username cliConf.Password = password config := ssh.ClientConfig{ User: cliConf.Username, Auth: []ssh.AuthMethod{ssh.Password(password)}, HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error { return nil }, Timeout: 10 * time.Second, } addr := fmt.Sprintf("%s:%d", cliConf.Host, cliConf.Port) if sshClient, err = ssh.Dial("tcp", addr, &config); err != nil { log.Fatalln("error occurred:", err) } cliConf.sshClient = sshClient //此时获取了sshClient,下面使用sshClient构建sftpClient if sftpClient, err = sftp.NewClient(sshClient); err != nil { log.Fatalln("error occurred:", err) } cliConf.sftpClient = sftpClient } func (cliConf *ClientConfig) RunShell(shell string) string { var ( session *ssh.Session err error ) //获取session,这个session是用来远程执行操作的 if session, err = cliConf.sshClient.NewSession(); err != nil { log.Fatalln("error occurred:", err) } //执行shell if output, err := session.CombinedOutput(shell); err != nil { fmt.Println(shell) log.Fatalln("error occurred:", err) } else { cliConf.LastResult = string(output) } return cliConf.LastResult } func (cliConf *ClientConfig) Upload(srcPath, dstPath string){ srcFile, _ := os.Open(srcPath) //本地 dstFile, _ := cliConf.sftpClient.Create(dstPath) //远程 defer func() { _ = srcFile.Close() _ = dstFile.Close() }() buf := make([]byte, 1024) for { n, err := srcFile.Read(buf) if err != nil { if err != io.EOF { log.Fatalln("error occurred:",err) } else { break } } _, _ = dstFile.Write(buf[: n]) } fmt.Println(cliConf.RunShell(fmt.Sprintf("ls %s", dstPath))) } func (cliConf *ClientConfig) Download(srcPath, dstPath string){ srcFile, err := cliConf.sftpClient.Open(srcPath) //远程 fmt.Println(srcFile) if err!=nil { fmt.Println(err.Error()) } dstFile, _ := os.Create(dstPath) //本地 defer func() { _ = srcFile.Close() _ = dstFile.Close() }() if _, err := srcFile.WriteTo(dstFile); err != nil { log.Fatalln("error occurred", err) } fmt.Println("文件下载完毕") } func main() { cliConf := new(ClientConfig) cliConf.createClient("", 22, "user", "passwd!") //本地文件上传到服务器 cliConf.Upload("./", "/root/test/") //从服务器中下载文件 cliConf.Download("/root/test/", "./") }