Skip to content

Commit

Permalink
fix(v1.0): support file upload md5 validate
Browse files Browse the repository at this point in the history
  • Loading branch information
cuisongliu committed Mar 7, 2020
1 parent 0c3ab95 commit 58324b8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
6 changes: 3 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ var rootCmd = &cobra.Command{
case "ssh":
sshType.Cmd(host, command)
case "scp":
sshType.Copy(host, localFilePath, remoteFilePath)
sshType.CopyForMD5(host, localFilePath, remoteFilePath, "")
case "ssh|scp":
sshType.Cmd(host, command)
sshType.Copy(host, localFilePath, remoteFilePath)
sshType.CopyForMD5(host, localFilePath, remoteFilePath, "")
case "scp|ssh":
sshType.Copy(host, localFilePath, remoteFilePath)
sshType.CopyForMD5(host, localFilePath, remoteFilePath, "")
sshType.Cmd(host, command)
default:
sshType.Cmd(host, command)
Expand Down
28 changes: 28 additions & 0 deletions pkg/sshutil/scp.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,42 @@
package sshutil

import (
"fmt"
"github.com/pkg/sftp"
"github.com/wonderivan/logger"
"golang.org/x/crypto/ssh"
"net"
"os"
"os/exec"
"time"
)

//Copy is
func (ss *SSH) CopyForMD5(host, localFilePath, remoteFilePath, md5 string) bool {
//如果有md5则可以验证
//如果没有md5则拿到本地数据后验证
if md5 == "" {
cmd := fmt.Sprintf("md5sum %s | cut -d\" \" -f1", localFilePath)
c := exec.Command("sh", "-c", cmd)
out, err := c.CombinedOutput()
if err != nil {
logger.Error(err)
}
md5 = string(out)
}
logger.Debug("source file md5 value is %s", md5)
ss.Copy(host, localFilePath, remoteFilePath)
cmd := fmt.Sprintf("md5sum %s | cut -d\" \" -f1", remoteFilePath)
remoteMD5 := ss.CmdToString(host, cmd)
logger.Debug("host: %s , remote md5: %s", host, remoteMD5)
if remoteMD5 == md5 {
logger.Info("md5 validate true")
return true
}
logger.Error("md5 validate false")
return false
}

//Copy is
func (ss *SSH) Copy(host, localFilePath, remoteFilePath string) {
sftpClient, err := ss.sftpConnect(host)
Expand Down

0 comments on commit 58324b8

Please sign in to comment.