From be040542bbb9111edc310d5270782b37564b06f6 Mon Sep 17 00:00:00 2001 From: Starainrt Date: Mon, 17 May 2021 15:11:24 +0800 Subject: [PATCH] fix --- sftp.go | 60 +++++++++++++++- ssh.go | 210 ++++++++++++++++++++++++++++---------------------------- 2 files changed, 164 insertions(+), 106 deletions(-) diff --git a/sftp.go b/sftp.go index 1d19907..6b1c031 100644 --- a/sftp.go +++ b/sftp.go @@ -7,6 +7,19 @@ import ( "os" ) +func (star *StarSSH) CreateSftpClient() (*sftp.Client, error) { + return sftp.NewClient(star.Client) +} + +func (star *StarSSH) SftpTransferOut(localFilePath, remotePath string) error { + sftpC, err := star.CreateSftpClient() + if err != nil { + return err + } + defer sftpC.Close() + return SftpTransferOut(localFilePath, remotePath, sftpC) +} + func SftpTransferOut(localFilePath, remotePath string, sftpClient *sftp.Client) error { srcFile, err := os.Open(localFilePath) if err != nil { @@ -30,6 +43,15 @@ func SftpTransferOut(localFilePath, remotePath string, sftpClient *sftp.Client) return nil } +func (star *StarSSH) SftpTransferOutByte(localData []byte, remotePath string) error { + sftpC, err := star.CreateSftpClient() + if err != nil { + return err + } + defer sftpC.Close() + return SftpTransferOutByte(localData, remotePath, sftpC) +} + func SftpTransferOutByte(localData []byte, remotePath string, sftpClient *sftp.Client) error { dstFile, err := sftpClient.Create(remotePath) if err != nil { @@ -40,6 +62,15 @@ func SftpTransferOutByte(localData []byte, remotePath string, sftpClient *sftp.C return err } +func (star *StarSSH) SftpTransferOutFunc(localFilePath, remotePath string, bufcap int, rtefunc func(float64)) error { + sftpC, err := star.CreateSftpClient() + if err != nil { + return err + } + defer sftpC.Close() + return SftpTransferOutFunc(localFilePath, remotePath, bufcap, rtefunc, sftpC) +} + func SftpTransferOutFunc(localFilePath, remotePath string, bufcap int, rtefunc func(float64), sftpClient *sftp.Client) error { num := 0 srcFile, err := os.Open(localFilePath) @@ -71,6 +102,15 @@ func SftpTransferOutFunc(localFilePath, remotePath string, bufcap int, rtefunc f return nil } +func (star *StarSSH) SftpTransferInByte(remotePath string) ([]byte, error) { + sftpC, err := star.CreateSftpClient() + if err != nil { + return nil, err + } + defer sftpC.Close() + return SftpTransferInByte(remotePath, sftpC) +} + func SftpTransferInByte(remotePath string, sftpClient *sftp.Client) ([]byte, error) { dstFile, err := sftpClient.Open(remotePath) if err != nil { @@ -82,6 +122,15 @@ func SftpTransferInByte(remotePath string, sftpClient *sftp.Client) ([]byte, err return buf.Bytes(), err } +func (star *StarSSH) SftpTransferIn(src, dst string) error { + sftpC, err := star.CreateSftpClient() + if err != nil { + return err + } + defer sftpC.Close() + return SftpTransferIn(src, dst, sftpC) +} + func SftpTransferIn(src, dst string, sftpClient *sftp.Client) error { srcFile, err := sftpClient.Open(src) if err != nil { @@ -102,6 +151,15 @@ func SftpTransferIn(src, dst string, sftpClient *sftp.Client) error { return nil } +func (star *StarSSH) SftpTransferInFunc(src, dst string, bufcap int, rtefunc func(float64)) error { + sftpC, err := star.CreateSftpClient() + if err != nil { + return err + } + defer sftpC.Close() + return SftpTransferInFunc(src, dst, bufcap, rtefunc, sftpC) +} + func SftpTransferInFunc(src, dst string, bufcap int, rtefunc func(float64), sftpClient *sftp.Client) error { num := 0 srcFile, err := sftpClient.Open(src) @@ -131,4 +189,4 @@ func SftpTransferInFunc(src, dst string, bufcap int, rtefunc func(float64), sftp } } return nil -} \ No newline at end of file +} diff --git a/ssh.go b/ssh.go index e700640..e2f07e4 100644 --- a/ssh.go +++ b/ssh.go @@ -178,24 +178,24 @@ func LoginSimple(host string, user string, passwd string, prikeyPath string, por return Login(info) } -func (this *StarShell) ShellWait(cmd string) (string, string, error) { +func (s *StarShell) ShellWait(cmd string) (string, string, error) { var outc, errc string = " ", " " - this.Clear() - defer this.Clear() + s.Clear() + defer s.Clear() echo := "echo b7Y85R56TUY6R5UTb612" - err := this.WriteCommand(cmd) + err := s.WriteCommand(cmd) if err != nil { return "", "", err } time.Sleep(time.Millisecond * 20) - err = this.WriteCommand(echo) + err = s.WriteCommand(echo) if err != nil { return "", "", err } for { time.Sleep(time.Millisecond * 120) - outs := string(this.outbyte) - errs := string(this.errbyte) + outs := string(s.outbyte) + errs := string(s.errbyte) outs = strings.TrimSpace(strings.ReplaceAll(outs, "\r\n", "\n")) errs = strings.TrimSpace(strings.ReplaceAll(errs, "\r\n", "\n")) if len(outs) >= len(cmd+"\n"+echo) && outs[0:len(cmd+"\n"+echo)] == cmd+"\n"+echo { @@ -206,7 +206,7 @@ func (this *StarShell) ShellWait(cmd string) (string, string, error) { if len(errs) >= len(cmd) && errs[0:len(cmd)] == cmd { errs = errs[len(cmd):] } - if this.UseWaitDefault { + if s.UseWaitDefault { if strings.Index(string(outs), "b7Y85R56TUY6R5UTb612") >= 0 { list := strings.Split(string(outs), "\n") for _, v := range list { @@ -226,20 +226,20 @@ func (this *StarShell) ShellWait(cmd string) (string, string, error) { break } } - if this.Keyword != "" { - if strings.Index(string(outs), this.Keyword) >= 0 { + if s.Keyword != "" { + if strings.Index(string(outs), s.Keyword) >= 0 { list := strings.Split(string(outs), "\n") for _, v := range list { - if strings.Index(v, this.Keyword) < 0 && strings.Index(v, "b7Y85R56TUY6R5UTb612") < 0 { + if strings.Index(v, s.Keyword) < 0 && strings.Index(v, "b7Y85R56TUY6R5UTb612") < 0 { outc += v + "\n" } } break } - if strings.Index(string(errs), this.Keyword) >= 0 { + if strings.Index(string(errs), s.Keyword) >= 0 { list := strings.Split(string(errs), "\n") for _, v := range list { - if strings.Index(v, this.Keyword) < 0 && strings.Index(v, "b7Y85R56TUY6R5UTb612") < 0 { + if strings.Index(v, s.Keyword) < 0 && strings.Index(v, "b7Y85R56TUY6R5UTb612") < 0 { errc += v + "\n" } } @@ -247,23 +247,23 @@ func (this *StarShell) ShellWait(cmd string) (string, string, error) { } } } - return this.TrimColor(strings.TrimSpace(outc)), this.TrimColor(strings.TrimSpace(errc)), err + return s.TrimColor(strings.TrimSpace(outc)), s.TrimColor(strings.TrimSpace(errc)), err } -func (this *StarShell) Close() error { - return this.Session.Close() +func (s *StarShell) Close() error { + return s.Session.Close() } -func (this *StarShell) SwitchNoColor(is bool) { - this.iscolor = is +func (s *StarShell) SwitchNoColor(is bool) { + s.iscolor = is } -func (this *StarShell) SwitchEcho(is bool) { - this.isecho = is +func (s *StarShell) SwitchEcho(is bool) { + s.isecho = is } -func (this *StarShell) TrimColor(str string) string { - if this.iscolor { +func (s *StarShell) TrimColor(str string) string { + if s.iscolor { return SedColor(str) } return str @@ -272,42 +272,42 @@ func (this *StarShell) TrimColor(str string) string { /* 本函数控制是否在本地屏幕上打印远程Shell的输出内容[true|false] */ -func (this *StarShell) SwitchPrint(run bool) { - this.isprint = run +func (s *StarShell) SwitchPrint(run bool) { + s.isprint = run } /* 本函数控制是否立即处理远程Shell输出每一行内容[true|false] */ -func (this *StarShell) SwitchFunc(run bool) { - this.isfuncs = run +func (s *StarShell) SwitchFunc(run bool) { + s.isfuncs = run } -func (this *StarShell) SetFunc(funcs func(string)) { - this.funcs = funcs +func (s *StarShell) SetFunc(funcs func(string)) { + s.funcs = funcs } -func (this *StarShell) Clear() { - defer this.rw.Unlock() - this.rw.Lock() - this.outbyte = []byte{} - this.errbyte = []byte{} +func (s *StarShell) Clear() { + defer s.rw.Unlock() + s.rw.Lock() + s.outbyte = []byte{} + s.errbyte = []byte{} time.Sleep(time.Millisecond * 15) } -func (this *StarShell) ShellClear(cmd string, sleep int) (string, string, error) { - defer this.Clear() - this.Clear() - return this.Shell(cmd, sleep) +func (s *StarShell) ShellClear(cmd string, sleep int) (string, string, error) { + defer s.Clear() + s.Clear() + return s.Shell(cmd, sleep) } -func (this *StarShell) Shell(cmd string, sleep int) (string, string, error) { - if err := this.WriteCommand(cmd); err != nil { +func (s *StarShell) Shell(cmd string, sleep int) (string, string, error) { + if err := s.WriteCommand(cmd); err != nil { return "", "", err } - tmp1, tmp2, err := this.GetResult(sleep) - tmps := this.TrimColor(strings.TrimSpace(string(tmp1))) - if this.isecho { + tmp1, tmp2, err := s.GetResult(sleep) + tmps := s.TrimColor(strings.TrimSpace(string(tmp1))) + if s.isecho { n := len(strings.Split(cmd, "\n")) if n == 1 { list := strings.SplitN(tmps, "\n", 2) @@ -334,50 +334,50 @@ func (this *StarShell) Shell(cmd string, sleep int) (string, string, error) { tmps = tmps[0 : len(tmps)-1] } } - return tmps, this.TrimColor(strings.TrimSpace(string(tmp2))), err + return tmps, s.TrimColor(strings.TrimSpace(string(tmp2))), err } -func (this *StarShell) GetResult(sleep int) ([]byte, []byte, error) { - if this.errors != nil { - this.Session.Close() - return this.outbyte, this.errbyte, this.errors +func (s *StarShell) GetResult(sleep int) ([]byte, []byte, error) { + if s.errors != nil { + s.Session.Close() + return s.outbyte, s.errbyte, s.errors } if sleep > 0 { time.Sleep(time.Millisecond * time.Duration(sleep)) } - return this.outbyte, this.errbyte, nil + return s.outbyte, s.errbyte, nil } -func (this *StarShell) WriteCommand(cmd string) error { - return this.Write([]byte(cmd + "\n")) +func (s *StarShell) WriteCommand(cmd string) error { + return s.Write([]byte(cmd + "\n")) } -func (this *StarShell) Write(bstr []byte) error { - if this.errors != nil { - this.Session.Close() - return this.errors +func (s *StarShell) Write(bstr []byte) error { + if s.errors != nil { + s.Session.Close() + return s.errors } - _, err := this.in.Write(bstr) + _, err := s.in.Write(bstr) return err } -func (this *StarShell) gohub() { +func (s *StarShell) gohub() { go func() { var cache []byte for { - read, err := this.er.ReadByte() + read, err := s.er.ReadByte() if err != nil { - this.errors = err + s.errors = err return } - this.errbyte = append(this.errbyte, read) - if this.isprint { + s.errbyte = append(s.errbyte, read) + if s.isprint { fmt.Print(string([]byte{read})) } cache = append(cache, read) if read == '\n' { - if this.isfuncs { - go this.funcs(this.TrimColor(strings.TrimSpace(string(cache)))) + if s.isfuncs { + go s.funcs(s.TrimColor(strings.TrimSpace(string(cache)))) cache = []byte{} } } @@ -385,47 +385,47 @@ func (this *StarShell) gohub() { }() var cache []byte for { - read, err := this.out.ReadByte() + read, err := s.out.ReadByte() if err != nil { - this.errors = err + s.errors = err return } - this.rw.Lock() - this.outbyte = append(this.outbyte, read) + s.rw.Lock() + s.outbyte = append(s.outbyte, read) cache = append(cache, read) - this.rw.Unlock() + s.rw.Unlock() if read == '\n' { - if this.isfuncs { - go this.funcs(strings.TrimSpace(string(cache))) + if s.isfuncs { + go s.funcs(strings.TrimSpace(string(cache))) cache = []byte{} } } - if this.isprint { + if s.isprint { fmt.Print(string([]byte{read})) } } } -func (this *StarShell) GetUid() string { - res, _, _ := this.ShellWait(`id | grep -oP "(?<=uid\=)\d+"`) +func (s *StarShell) GetUid() string { + res, _, _ := s.ShellWait(`id | grep -oP "(?<=uid\=)\d+"`) return strings.TrimSpace(res) } -func (this *StarShell) GetGid() string { - res, _, _ := this.ShellWait(`id | grep -oP "(?<=gid\=)\d+"`) +func (s *StarShell) GetGid() string { + res, _, _ := s.ShellWait(`id | grep -oP "(?<=gid\=)\d+"`) return strings.TrimSpace(res) } -func (this *StarShell) GetUser() string { - res, _, _ := this.ShellWait(`id | grep -oP "(?<=\().*?(?=\))" | head -n 1`) +func (s *StarShell) GetUser() string { + res, _, _ := s.ShellWait(`id | grep -oP "(?<=\().*?(?=\))" | head -n 1`) return strings.TrimSpace(res) } -func (this *StarShell) GetGroup() string { - res, _, _ := this.ShellWait(`id | grep -oP "(?<=\().*?(?=\))" | head -n 2 | tail -n 1`) +func (s *StarShell) GetGroup() string { + res, _, _ := s.ShellWait(`id | grep -oP "(?<=\().*?(?=\))" | head -n 2 | tail -n 1`) return strings.TrimSpace(res) } -func (this *StarSSH) NewShell() (shell *StarShell, err error) { +func (s *StarSSH) NewShell() (shell *StarShell, err error) { shell = new(StarShell) - shell.Session, err = this.NewSession() + shell.Session, err = s.NewSession() if err != nil { return } @@ -449,19 +449,19 @@ func (this *StarSSH) NewShell() (shell *StarShell, err error) { return } -func (this *StarSSH) Close() error { - if this.online { - return this.Client.Close() +func (s *StarSSH) Close() error { + if s.online { + return s.Client.Close() } return nil } -func (this *StarSSH) NewSession() (*ssh.Session, error) { - return NewSession(this.Client) +func (s *StarSSH) NewSession() (*ssh.Session, error) { + return NewSession(s.Client) } -func (this *StarSSH) ShellOne(cmd string) (string, error) { - newsess, err := this.NewSession() +func (s *StarSSH) ShellOne(cmd string) (string, error) { + newsess, err := s.NewSession() if err != nil { return "", err } @@ -470,8 +470,8 @@ func (this *StarSSH) ShellOne(cmd string) (string, error) { return strings.TrimSpace(string(data)), err } -func (this *StarSSH) Exists(filepath string) bool { - res, _ := this.ShellOne(`echo 1 && [ ! -e "` + filepath + `" ] && echo 2`) +func (s *StarSSH) Exists(filepath string) bool { + res, _ := s.ShellOne(`echo 1 && [ ! -e "` + filepath + `" ] && echo 2`) if res == "1" { return true } else { @@ -479,25 +479,25 @@ func (this *StarSSH) Exists(filepath string) bool { } } -func (this *StarSSH) GetUid() string { - res, _ := this.ShellOne(`id | grep -oP "(?<=uid\=)\d+"`) +func (s *StarSSH) GetUid() string { + res, _ := s.ShellOne(`id | grep -oP "(?<=uid\=)\d+"`) return strings.TrimSpace(res) } -func (this *StarSSH) GetGid() string { - res, _ := this.ShellOne(`id | grep -oP "(?<=gid\=)\d+"`) +func (s *StarSSH) GetGid() string { + res, _ := s.ShellOne(`id | grep -oP "(?<=gid\=)\d+"`) return strings.TrimSpace(res) } -func (this *StarSSH) GetUser() string { - res, _ := this.ShellOne(`id | grep -oP "(?<=\().*?(?=\))" | head -n 1`) +func (s *StarSSH) GetUser() string { + res, _ := s.ShellOne(`id | grep -oP "(?<=\().*?(?=\))" | head -n 1`) return strings.TrimSpace(res) } -func (this *StarSSH) GetGroup() string { - res, _ := this.ShellOne(`id | grep -oP "(?<=\().*?(?=\))" | head -n 2 | tail -n 1`) +func (s *StarSSH) GetGroup() string { + res, _ := s.ShellOne(`id | grep -oP "(?<=\().*?(?=\))" | head -n 2 | tail -n 1`) return strings.TrimSpace(res) } -func (this *StarSSH) IsFile(filepath string) bool { - res, _ := this.ShellOne(`echo 1 && [ ! -f "` + filepath + `" ] && echo 2`) +func (s *StarSSH) IsFile(filepath string) bool { + res, _ := s.ShellOne(`echo 1 && [ ! -f "` + filepath + `" ] && echo 2`) if res == "1" { return true } else { @@ -505,8 +505,8 @@ func (this *StarSSH) IsFile(filepath string) bool { } } -func (this *StarSSH) IsFolder(filepath string) bool { - res, _ := this.ShellOne(`echo 1 && [ ! -d "` + filepath + `" ] && echo 2`) +func (s *StarSSH) IsFolder(filepath string) bool { + res, _ := s.ShellOne(`echo 1 && [ ! -d "` + filepath + `" ] && echo 2`) if res == "1" { return true } else { @@ -514,8 +514,8 @@ func (this *StarSSH) IsFolder(filepath string) bool { } } -func (this *StarSSH) ShellOneShowScreen(cmd string) (string, error) { - newsess, err := this.NewSession() +func (s *StarSSH) ShellOneShowScreen(cmd string) (string, error) { + newsess, err := s.NewSession() if err != nil { return "", err } @@ -559,8 +559,8 @@ func (this *StarSSH) ShellOneShowScreen(cmd string) (string, error) { return strings.TrimSpace(string(bytes)), err } -func (this *StarSSH) ShellOneToFunc(cmd string, callback func(string)) (string, error) { - newsess, err := this.NewSession() +func (s *StarSSH) ShellOneToFunc(cmd string, callback func(string)) (string, error) { + newsess, err := s.NewSession() if err != nil { return "", err }