From 4a21bb48bf346d24c627e732c7c12a33725ce910 Mon Sep 17 00:00:00 2001 From: ren yuze Date: Thu, 7 Mar 2019 11:37:46 +0800 Subject: [PATCH] March Update --- ssh.go | 39 +++++++++++++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/ssh.go b/ssh.go index f8558f9..818a283 100644 --- a/ssh.go +++ b/ssh.go @@ -19,7 +19,7 @@ import ( var ShellRes, ShellErr string var ShellExit bool -type sshd struct { +type Sshd struct { SSHC *ssh.Session infile io.Writer outfile io.Reader @@ -152,7 +152,30 @@ func CreateSftp(client *ssh.Client) (*sftp.Client, error) { return sftpClient, err } -func FtpTransfer(src, dst string, sftpClient *sftp.Client) error { +func FtpTransferOut(localFilePath, remoteDir string, sftpClient *sftp.Client) error { + srcFile, err := os.Open(localFilePath) + if err != nil { + return err + } + defer srcFile.Close() + var remoteFileName = path.Base(localFilePath) + dstFile, err := sftpClient.Create(path.Join(remoteDir, remoteFileName)) + if err != nil { + return err + } + defer dstFile.Close() + for { + buf := make([]byte, 1024) + n, _ := srcFile.Read(buf) + if n == 0 { + break + } + dstFile.Write(buf) + } + return nil +} + +func FtpTransferIn(src, dst string, sftpClient *sftp.Client) error { srcFile, err := sftpClient.Open(src) if err != nil { return err @@ -183,9 +206,9 @@ func Command(session *ssh.Session, cmdstr string) (string, error) { return res.String(), nil } -func SSHPipeShell(session *ssh.Session, cmdstr string) (*sshd, error) { +func SSHPipeShell(session *ssh.Session, cmdstr string) (*Sshd, error) { var err error - lovessh := sshd{} + lovessh := Sshd{} lovessh.SSHC = session lovessh.infile, err = lovessh.SSHC.StdinPipe() if err != nil { @@ -216,7 +239,7 @@ func SedColor(str string) string { return string(reg.ReplaceAll([]byte(str), []byte(""))) } -func (this sshd) GetResult(maxtime int) (string, string, bool) { +func (this Sshd) GetResult(maxtime int) (string, string, bool) { var stop bool reader := bufio.NewReader(this.outfile) erreader := bufio.NewReader(this.errfile) @@ -271,16 +294,16 @@ func (this sshd) GetResult(maxtime int) (string, string, bool) { return restr, errstr, true } -func (this sshd) Exec(cmdstr string, maxtime int) (string, string, bool) { +func (this Sshd) Exec(cmdstr string, maxtime int) (string, string, bool) { this.infile.Write([]byte(cmdstr + "\n")) return this.GetResult(maxtime) } -func (this sshd) WriteCmd(cmdstr string) { +func (this Sshd) WriteCmd(cmdstr string) { this.infile.Write([]byte(cmdstr + "\n")) return } -func (this sshd) IsExit() bool { +func (this Sshd) IsExit() bool { return ShellExit }