go语言实现sftp包上传文件和文件夹到远程服务器操作

 更新时间:2020年12月18日 09:20:41   作者:奔流入海  
这篇文章主要介绍了go语言实现sftp包上传文件和文件夹到远程服务器操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

使用go语言的第三方包:github.com/pkg/sftp和golang.org/x/crypto/ssh实现文件和文件夹传输。

1、创建connect方法:

func connect(user, password, host string, port int) (*sftp.Client, error) {
 var (
  auth   []ssh.AuthMethod
  addr   string
  clientConfig *ssh.ClientConfig
  sshClient *ssh.Client
  sftpClient *sftp.Client
  err   error
 )
 // get auth method
 auth = make([]ssh.AuthMethod, 0)
 auth = append(auth, ssh.Password(password))
 clientConfig = &ssh.ClientConfig{
  User:   user,
  Auth:   auth,
  Timeout:   30 * time.Second,
  HostKeyCallback: ssh.InsecureIgnoreHostKey(), //ssh.FixedHostKey(hostKey),
 }
 // connet to ssh
 addr = fmt.Sprintf("%s:%d", host, port)
 if sshClient, err = ssh.Dial("tcp", addr, clientConfig); err != nil {
  return nil, err
 }
 // create sftp client
 if sftpClient, err = sftp.NewClient(sshClient); err != nil {
  return nil, err
 }
 return sftpClient, nil
}

2、上传文件

func uploadFile(sftpClient *sftp.Client, localFilePath string, remotePath string) {
 srcFile, err := os.Open(localFilePath)
 if err != nil {
  fmt.Println("os.Open error : ", localFilePath)
  log.Fatal(err)
 }
 defer srcFile.Close()
 var remoteFileName = path.Base(localFilePath)
 dstFile, err := sftpClient.Create(path.Join(remotePath, remoteFileName))
 if err != nil {
  fmt.Println("sftpClient.Create error : ", path.Join(remotePath, remoteFileName))
  log.Fatal(err)
 }
 defer dstFile.Close()
 ff, err := ioutil.ReadAll(srcFile)
 if err != nil {
  fmt.Println("ReadAll error : ", localFilePath)
  log.Fatal(err)
 }
 dstFile.Write(ff)
 fmt.Println(localFilePath + " copy file to remote server finished!")
}

3、上传文件夹

func uploadDirectory(sftpClient *sftp.Client, localPath string, remotePath string) {
 localFiles, err := ioutil.ReadDir(localPath)
 if err != nil {
  log.Fatal("read dir list fail ", err)
 }
 for _, backupDir := range localFiles {
  localFilePath := path.Join(localPath, backupDir.Name())
  remoteFilePath := path.Join(remotePath, backupDir.Name())
  if backupDir.IsDir() {
   sftpClient.Mkdir(remoteFilePath)
   uploadDirectory(sftpClient, localFilePath, remoteFilePath)
  } else {
   uploadFile(sftpClient, path.Join(localPath, backupDir.Name()), remotePath)
  }
 }
 fmt.Println(localPath + " copy directory to remote server finished!")
}

4、上传测试

func DoBackup(host string, port int, userName string, password string, localPath string, remotePath string) {
 var (
  err  error
  sftpClient *sftp.Client
 )
 start := time.Now()
 sftpClient, err = connect(userName, password, host, port)
 if err != nil {
  log.Fatal(err)
 }
 defer sftpClient.Close()
 _, errStat := sftpClient.Stat(remotePath)
 if errStat != nil {
  log.Fatal(remotePath + " remote path not exists!")
 }
 backupDirs, err := ioutil.ReadDir(localPath)
 if err != nil {
  log.Fatal(localPath + " local path not exists!")
 }
 uploadDirectory(sftpClient, localPath, remotePath)
 elapsed := time.Since(start)
 fmt.Println("elapsed time : ", elapsed)
}

补充:go实现ssh远程机器并传输文件

核心依赖包:

golang.org/x/crypto/ssh

github.com/pkg/sftp

其中golang.org/x/crypto/ssh 可从github上下载,

下载地址:https://github.com/golang/crypto

ssh连接源码(这里是根据秘钥连接):

var keypath = "key/id_rsa"
//获取秘钥
func publicKey(path string) ssh.AuthMethod {
 keypath, err := homedir.Expand(path)
 if err != nil {
 fmt.Println("获取秘钥路径失败", err)
 }
 key, err1 := ioutil.ReadFile(keypath)
 if err1 != nil {
 fmt.Println("读取秘钥失败", err1)
 }
 signer, err2 := ssh.ParsePrivateKey(key)
 if err2 != nil {
 fmt.Println("ssh 秘钥签名失败", err2)
 }
 return ssh.PublicKeys(signer)
}
//获取ssh连接
func GetSSHConect(ip, user string, port int) (*ssh.Client) {
 con := &ssh.ClientConfig{
 User: user,
 Auth: []ssh.AuthMethod{publicKey(keypath)},
 HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
 return nil
 },
 }
 addr := fmt.Sprintf("%s:%d", ip, port)
 client, err := ssh.Dial("tcp", addr, con)
 if err != nil {
 fmt.Println("Dail failed: ", err)
 panic(err)
 }
 return client
}
// 远程执行脚本
func Exec_Task(ip, user, localpath, remotepath string) int {
 port := 22
 client := GetSSHConect(ip, user, port)
 UploadFile(ip, user, localpath, remotepath, port)
 session, err := client.NewSession()
 if err != nil {
 fmt.Println("创建会话失败", err)
 panic(err)
 }
 defer session.Close()
 remoteFileName := path.Base(localpath)
 dstFile := path.Join(remotepath, remoteFileName)
 err1 := session.Run(fmt.Sprintf("/usr/bin/sh %s", dstFile))
 if err1 != nil {
 fmt.Println("远程执行脚本失败", err1)
 return 2
 } else {
 fmt.Println("远程执行脚本成功")
 return 1
 }
}

文件传输功能:

//获取ftp连接
func getftpclient(client *ssh.Client) (*sftp.Client) {
 ftpclient, err := sftp.NewClient(client)
 if err != nil {
 fmt.Println("创建ftp客户端失败", err)
 panic(err)
 }
 return ftpclient
}
//上传文件
func UploadFile(ip, user, localpath, remotepath string, port int) {
 client := GetSSHConect(ip, user, port)
 ftpclient := getftpclient(client)
 defer ftpclient.Close()
 remoteFileName := path.Base(localpath)
 fmt.Println(localpath, remoteFileName)
 srcFile, err := os.Open(localpath)
 if err != nil {
 fmt.Println("打开文件失败", err)
 panic(err)
 }
 defer srcFile.Close()
 dstFile, e := ftpclient.Create(path.Join(remotepath, remoteFileName))
 if e != nil {
 fmt.Println("创建文件失败", e)
 panic(e)
 }
 defer dstFile.Close()
 buffer := make([]byte, 1024)
 for {
 n, err := srcFile.Read(buffer)
 if err != nil {
 if err == io.EOF {
 fmt.Println("已读取到文件末尾")
 break
 } else {
 fmt.Println("读取文件出错", err)
 panic(err)
 }
 }
 dstFile.Write(buffer[:n]) 
 //注意,由于文件大小不定,不可直接使用buffer,否则会在文件末尾重复写入,以填充1024的整数倍
 }
 fmt.Println("文件上传成功")
}
//文件下载
func DownLoad(ip, user, localpath, remotepath string, port int) {
 client := GetSSHConect(ip, user, port)
 ftpClient := getftpclient(client)
 defer ftpClient.Close()
 srcFile, err := ftpClient.Open(remotepath)
 if err != nil {
 fmt.Println("文件读取失败", err)
 panic(err)
 }
 defer srcFile.Close()
 localFilename := path.Base(remotepath)
 dstFile, e := os.Create(path.Join(localpath, localFilename))
 if e != nil {
 fmt.Println("文件创建失败", e)
 panic(e)
 }
 defer dstFile.Close()
 if _, err1 := srcFile.WriteTo(dstFile); err1 != nil {
 fmt.Println("文件写入失败", err1)
 panic(err1)
 }
 fmt.Println("文件下载成功")
}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。如有错误或未考虑完全的地方,望不吝赐教。

相关文章

  • Go语言JSON解析器gjson使用方法详解

    Go语言JSON解析器gjson使用方法详解

    这篇文章主要介绍了Go语言json解析框架与gjson,JSON 解析是我们不可避免的常见问题,在Go语言中,我们可以借助gjson库来方便的进行json属性的提取与解析,需要的朋友可以参考一下
    2022-12-12
  • Golang中优秀的消息队列NSQ基础安装及使用详解

    Golang中优秀的消息队列NSQ基础安装及使用详解

    这篇文章主要介绍了Golang中优秀的消息队列NSQ基础安装及使用详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-12-12
  • 解决golang http.FileServer 遇到的坑

    解决golang http.FileServer 遇到的坑

    这篇文章主要介绍了解决golang http.FileServer 遇到的坑,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-12-12
  • 详解Golang如何优雅判断interface是否为nil

    详解Golang如何优雅判断interface是否为nil

    这篇文章主要为大家详细介绍了Golang如何优雅判断interface是否为nil的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起了解下
    2024-01-01
  • golang 对象深拷贝的常见方式及性能

    golang 对象深拷贝的常见方式及性能

    这篇文章主要介绍了golang 对象深拷贝的常见方式及性能,Go语言中所有赋值操作都是值传递,如果结构中不含指针,则直接赋值就是深度拷贝,文章围绕主题展开更多相关资料,需要的小伙伴可以参考一下
    2022-06-06
  • Golang开发中常用的代码片段汇总

    Golang开发中常用的代码片段汇总

    这篇文章主要给大家汇总了在Golang开发中常用的代码片段,这些代码片段都是在日常工作中编写golang应用时使用到,需要的朋友可以参考借鉴,下面跟着小编一起来学习学习吧。
    2017-07-07
  • Golang使用Gin框架实现路由分类处理请求流程详解

    Golang使用Gin框架实现路由分类处理请求流程详解

    Gin是一个golang的微框架,封装比较优雅,具有快速灵活,容错方便等特点,这篇文章主要介绍了Golang使用Gin框架实现路由分类处理请求,感兴趣的同学可以参考下文
    2023-05-05
  • go使用makefile脚本编译应用的方法小结

    go使用makefile脚本编译应用的方法小结

    makefile可以看作是make工具的脚本文件, 而make主要用来处理一系列命令。常用的比如用来编译和打包文件, 在C/C++的编译打包中应用最广泛了,这篇文章主要介绍了go使用makefile脚本编译应用,需要的朋友可以参考下
    2022-08-08
  • 使用docker构建golang线上部署环境的步骤详解

    使用docker构建golang线上部署环境的步骤详解

    这篇文章主要介绍了使用docker构建golang线上部署环境的步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。
    2017-11-11
  • Go语言底层原理互斥锁的实现原理

    Go语言底层原理互斥锁的实现原理

    这篇文章主要介绍了Go语言底层原理互斥锁的实现原理,Go sync包提供了两种锁类型,分别是互斥锁sync.Mutex和读写互斥锁sync.RWMutex,都属于悲观锁,更多相关内容需要的朋友可以查看下面文章内容
    2022-08-08

最新评论