master
Starainrt 3 years ago
parent bcc27ff52f
commit be040542bb

@ -7,6 +7,19 @@ import (
"os" "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 { func SftpTransferOut(localFilePath, remotePath string, sftpClient *sftp.Client) error {
srcFile, err := os.Open(localFilePath) srcFile, err := os.Open(localFilePath)
if err != nil { if err != nil {
@ -30,6 +43,15 @@ func SftpTransferOut(localFilePath, remotePath string, sftpClient *sftp.Client)
return nil 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 { func SftpTransferOutByte(localData []byte, remotePath string, sftpClient *sftp.Client) error {
dstFile, err := sftpClient.Create(remotePath) dstFile, err := sftpClient.Create(remotePath)
if err != nil { if err != nil {
@ -40,6 +62,15 @@ func SftpTransferOutByte(localData []byte, remotePath string, sftpClient *sftp.C
return err 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 { func SftpTransferOutFunc(localFilePath, remotePath string, bufcap int, rtefunc func(float64), sftpClient *sftp.Client) error {
num := 0 num := 0
srcFile, err := os.Open(localFilePath) srcFile, err := os.Open(localFilePath)
@ -71,6 +102,15 @@ func SftpTransferOutFunc(localFilePath, remotePath string, bufcap int, rtefunc f
return nil 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) { func SftpTransferInByte(remotePath string, sftpClient *sftp.Client) ([]byte, error) {
dstFile, err := sftpClient.Open(remotePath) dstFile, err := sftpClient.Open(remotePath)
if err != nil { if err != nil {
@ -82,6 +122,15 @@ func SftpTransferInByte(remotePath string, sftpClient *sftp.Client) ([]byte, err
return buf.Bytes(), 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 { func SftpTransferIn(src, dst string, sftpClient *sftp.Client) error {
srcFile, err := sftpClient.Open(src) srcFile, err := sftpClient.Open(src)
if err != nil { if err != nil {
@ -102,6 +151,15 @@ func SftpTransferIn(src, dst string, sftpClient *sftp.Client) error {
return nil 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 { func SftpTransferInFunc(src, dst string, bufcap int, rtefunc func(float64), sftpClient *sftp.Client) error {
num := 0 num := 0
srcFile, err := sftpClient.Open(src) srcFile, err := sftpClient.Open(src)

210
ssh.go

@ -178,24 +178,24 @@ func LoginSimple(host string, user string, passwd string, prikeyPath string, por
return Login(info) 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 = " ", " " var outc, errc string = " ", " "
this.Clear() s.Clear()
defer this.Clear() defer s.Clear()
echo := "echo b7Y85R56TUY6R5UTb612" echo := "echo b7Y85R56TUY6R5UTb612"
err := this.WriteCommand(cmd) err := s.WriteCommand(cmd)
if err != nil { if err != nil {
return "", "", err return "", "", err
} }
time.Sleep(time.Millisecond * 20) time.Sleep(time.Millisecond * 20)
err = this.WriteCommand(echo) err = s.WriteCommand(echo)
if err != nil { if err != nil {
return "", "", err return "", "", err
} }
for { for {
time.Sleep(time.Millisecond * 120) time.Sleep(time.Millisecond * 120)
outs := string(this.outbyte) outs := string(s.outbyte)
errs := string(this.errbyte) errs := string(s.errbyte)
outs = strings.TrimSpace(strings.ReplaceAll(outs, "\r\n", "\n")) outs = strings.TrimSpace(strings.ReplaceAll(outs, "\r\n", "\n"))
errs = strings.TrimSpace(strings.ReplaceAll(errs, "\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 { 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 { if len(errs) >= len(cmd) && errs[0:len(cmd)] == cmd {
errs = errs[len(cmd):] errs = errs[len(cmd):]
} }
if this.UseWaitDefault { if s.UseWaitDefault {
if strings.Index(string(outs), "b7Y85R56TUY6R5UTb612") >= 0 { if strings.Index(string(outs), "b7Y85R56TUY6R5UTb612") >= 0 {
list := strings.Split(string(outs), "\n") list := strings.Split(string(outs), "\n")
for _, v := range list { for _, v := range list {
@ -226,20 +226,20 @@ func (this *StarShell) ShellWait(cmd string) (string, string, error) {
break break
} }
} }
if this.Keyword != "" { if s.Keyword != "" {
if strings.Index(string(outs), this.Keyword) >= 0 { if strings.Index(string(outs), s.Keyword) >= 0 {
list := strings.Split(string(outs), "\n") list := strings.Split(string(outs), "\n")
for _, v := range list { 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" outc += v + "\n"
} }
} }
break break
} }
if strings.Index(string(errs), this.Keyword) >= 0 { if strings.Index(string(errs), s.Keyword) >= 0 {
list := strings.Split(string(errs), "\n") list := strings.Split(string(errs), "\n")
for _, v := range list { 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" 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 { func (s *StarShell) Close() error {
return this.Session.Close() return s.Session.Close()
} }
func (this *StarShell) SwitchNoColor(is bool) { func (s *StarShell) SwitchNoColor(is bool) {
this.iscolor = is s.iscolor = is
} }
func (this *StarShell) SwitchEcho(is bool) { func (s *StarShell) SwitchEcho(is bool) {
this.isecho = is s.isecho = is
} }
func (this *StarShell) TrimColor(str string) string { func (s *StarShell) TrimColor(str string) string {
if this.iscolor { if s.iscolor {
return SedColor(str) return SedColor(str)
} }
return str return str
@ -272,42 +272,42 @@ func (this *StarShell) TrimColor(str string) string {
/* /*
Shell[true|false] Shell[true|false]
*/ */
func (this *StarShell) SwitchPrint(run bool) { func (s *StarShell) SwitchPrint(run bool) {
this.isprint = run s.isprint = run
} }
/* /*
Shell[true|false] Shell[true|false]
*/ */
func (this *StarShell) SwitchFunc(run bool) { func (s *StarShell) SwitchFunc(run bool) {
this.isfuncs = run s.isfuncs = run
} }
func (this *StarShell) SetFunc(funcs func(string)) { func (s *StarShell) SetFunc(funcs func(string)) {
this.funcs = funcs s.funcs = funcs
} }
func (this *StarShell) Clear() { func (s *StarShell) Clear() {
defer this.rw.Unlock() defer s.rw.Unlock()
this.rw.Lock() s.rw.Lock()
this.outbyte = []byte{} s.outbyte = []byte{}
this.errbyte = []byte{} s.errbyte = []byte{}
time.Sleep(time.Millisecond * 15) time.Sleep(time.Millisecond * 15)
} }
func (this *StarShell) ShellClear(cmd string, sleep int) (string, string, error) { func (s *StarShell) ShellClear(cmd string, sleep int) (string, string, error) {
defer this.Clear() defer s.Clear()
this.Clear() s.Clear()
return this.Shell(cmd, sleep) return s.Shell(cmd, sleep)
} }
func (this *StarShell) Shell(cmd string, sleep int) (string, string, error) { func (s *StarShell) Shell(cmd string, sleep int) (string, string, error) {
if err := this.WriteCommand(cmd); err != nil { if err := s.WriteCommand(cmd); err != nil {
return "", "", err return "", "", err
} }
tmp1, tmp2, err := this.GetResult(sleep) tmp1, tmp2, err := s.GetResult(sleep)
tmps := this.TrimColor(strings.TrimSpace(string(tmp1))) tmps := s.TrimColor(strings.TrimSpace(string(tmp1)))
if this.isecho { if s.isecho {
n := len(strings.Split(cmd, "\n")) n := len(strings.Split(cmd, "\n"))
if n == 1 { if n == 1 {
list := strings.SplitN(tmps, "\n", 2) 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] 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) { func (s *StarShell) GetResult(sleep int) ([]byte, []byte, error) {
if this.errors != nil { if s.errors != nil {
this.Session.Close() s.Session.Close()
return this.outbyte, this.errbyte, this.errors return s.outbyte, s.errbyte, s.errors
} }
if sleep > 0 { if sleep > 0 {
time.Sleep(time.Millisecond * time.Duration(sleep)) 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 { func (s *StarShell) WriteCommand(cmd string) error {
return this.Write([]byte(cmd + "\n")) return s.Write([]byte(cmd + "\n"))
} }
func (this *StarShell) Write(bstr []byte) error { func (s *StarShell) Write(bstr []byte) error {
if this.errors != nil { if s.errors != nil {
this.Session.Close() s.Session.Close()
return this.errors return s.errors
} }
_, err := this.in.Write(bstr) _, err := s.in.Write(bstr)
return err return err
} }
func (this *StarShell) gohub() { func (s *StarShell) gohub() {
go func() { go func() {
var cache []byte var cache []byte
for { for {
read, err := this.er.ReadByte() read, err := s.er.ReadByte()
if err != nil { if err != nil {
this.errors = err s.errors = err
return return
} }
this.errbyte = append(this.errbyte, read) s.errbyte = append(s.errbyte, read)
if this.isprint { if s.isprint {
fmt.Print(string([]byte{read})) fmt.Print(string([]byte{read}))
} }
cache = append(cache, read) cache = append(cache, read)
if read == '\n' { if read == '\n' {
if this.isfuncs { if s.isfuncs {
go this.funcs(this.TrimColor(strings.TrimSpace(string(cache)))) go s.funcs(s.TrimColor(strings.TrimSpace(string(cache))))
cache = []byte{} cache = []byte{}
} }
} }
@ -385,47 +385,47 @@ func (this *StarShell) gohub() {
}() }()
var cache []byte var cache []byte
for { for {
read, err := this.out.ReadByte() read, err := s.out.ReadByte()
if err != nil { if err != nil {
this.errors = err s.errors = err
return return
} }
this.rw.Lock() s.rw.Lock()
this.outbyte = append(this.outbyte, read) s.outbyte = append(s.outbyte, read)
cache = append(cache, read) cache = append(cache, read)
this.rw.Unlock() s.rw.Unlock()
if read == '\n' { if read == '\n' {
if this.isfuncs { if s.isfuncs {
go this.funcs(strings.TrimSpace(string(cache))) go s.funcs(strings.TrimSpace(string(cache)))
cache = []byte{} cache = []byte{}
} }
} }
if this.isprint { if s.isprint {
fmt.Print(string([]byte{read})) fmt.Print(string([]byte{read}))
} }
} }
} }
func (this *StarShell) GetUid() string { func (s *StarShell) GetUid() string {
res, _, _ := this.ShellWait(`id | grep -oP "(?<=uid\=)\d+"`) res, _, _ := s.ShellWait(`id | grep -oP "(?<=uid\=)\d+"`)
return strings.TrimSpace(res) return strings.TrimSpace(res)
} }
func (this *StarShell) GetGid() string { func (s *StarShell) GetGid() string {
res, _, _ := this.ShellWait(`id | grep -oP "(?<=gid\=)\d+"`) res, _, _ := s.ShellWait(`id | grep -oP "(?<=gid\=)\d+"`)
return strings.TrimSpace(res) return strings.TrimSpace(res)
} }
func (this *StarShell) GetUser() string { func (s *StarShell) GetUser() string {
res, _, _ := this.ShellWait(`id | grep -oP "(?<=\().*?(?=\))" | head -n 1`) res, _, _ := s.ShellWait(`id | grep -oP "(?<=\().*?(?=\))" | head -n 1`)
return strings.TrimSpace(res) return strings.TrimSpace(res)
} }
func (this *StarShell) GetGroup() string { func (s *StarShell) GetGroup() string {
res, _, _ := this.ShellWait(`id | grep -oP "(?<=\().*?(?=\))" | head -n 2 | tail -n 1`) res, _, _ := s.ShellWait(`id | grep -oP "(?<=\().*?(?=\))" | head -n 2 | tail -n 1`)
return strings.TrimSpace(res) return strings.TrimSpace(res)
} }
func (this *StarSSH) NewShell() (shell *StarShell, err error) { func (s *StarSSH) NewShell() (shell *StarShell, err error) {
shell = new(StarShell) shell = new(StarShell)
shell.Session, err = this.NewSession() shell.Session, err = s.NewSession()
if err != nil { if err != nil {
return return
} }
@ -449,19 +449,19 @@ func (this *StarSSH) NewShell() (shell *StarShell, err error) {
return return
} }
func (this *StarSSH) Close() error { func (s *StarSSH) Close() error {
if this.online { if s.online {
return this.Client.Close() return s.Client.Close()
} }
return nil return nil
} }
func (this *StarSSH) NewSession() (*ssh.Session, error) { func (s *StarSSH) NewSession() (*ssh.Session, error) {
return NewSession(this.Client) return NewSession(s.Client)
} }
func (this *StarSSH) ShellOne(cmd string) (string, error) { func (s *StarSSH) ShellOne(cmd string) (string, error) {
newsess, err := this.NewSession() newsess, err := s.NewSession()
if err != nil { if err != nil {
return "", err return "", err
} }
@ -470,8 +470,8 @@ func (this *StarSSH) ShellOne(cmd string) (string, error) {
return strings.TrimSpace(string(data)), err return strings.TrimSpace(string(data)), err
} }
func (this *StarSSH) Exists(filepath string) bool { func (s *StarSSH) Exists(filepath string) bool {
res, _ := this.ShellOne(`echo 1 && [ ! -e "` + filepath + `" ] && echo 2`) res, _ := s.ShellOne(`echo 1 && [ ! -e "` + filepath + `" ] && echo 2`)
if res == "1" { if res == "1" {
return true return true
} else { } else {
@ -479,25 +479,25 @@ func (this *StarSSH) Exists(filepath string) bool {
} }
} }
func (this *StarSSH) GetUid() string { func (s *StarSSH) GetUid() string {
res, _ := this.ShellOne(`id | grep -oP "(?<=uid\=)\d+"`) res, _ := s.ShellOne(`id | grep -oP "(?<=uid\=)\d+"`)
return strings.TrimSpace(res) return strings.TrimSpace(res)
} }
func (this *StarSSH) GetGid() string { func (s *StarSSH) GetGid() string {
res, _ := this.ShellOne(`id | grep -oP "(?<=gid\=)\d+"`) res, _ := s.ShellOne(`id | grep -oP "(?<=gid\=)\d+"`)
return strings.TrimSpace(res) return strings.TrimSpace(res)
} }
func (this *StarSSH) GetUser() string { func (s *StarSSH) GetUser() string {
res, _ := this.ShellOne(`id | grep -oP "(?<=\().*?(?=\))" | head -n 1`) res, _ := s.ShellOne(`id | grep -oP "(?<=\().*?(?=\))" | head -n 1`)
return strings.TrimSpace(res) return strings.TrimSpace(res)
} }
func (this *StarSSH) GetGroup() string { func (s *StarSSH) GetGroup() string {
res, _ := this.ShellOne(`id | grep -oP "(?<=\().*?(?=\))" | head -n 2 | tail -n 1`) res, _ := s.ShellOne(`id | grep -oP "(?<=\().*?(?=\))" | head -n 2 | tail -n 1`)
return strings.TrimSpace(res) return strings.TrimSpace(res)
} }
func (this *StarSSH) IsFile(filepath string) bool { func (s *StarSSH) IsFile(filepath string) bool {
res, _ := this.ShellOne(`echo 1 && [ ! -f "` + filepath + `" ] && echo 2`) res, _ := s.ShellOne(`echo 1 && [ ! -f "` + filepath + `" ] && echo 2`)
if res == "1" { if res == "1" {
return true return true
} else { } else {
@ -505,8 +505,8 @@ func (this *StarSSH) IsFile(filepath string) bool {
} }
} }
func (this *StarSSH) IsFolder(filepath string) bool { func (s *StarSSH) IsFolder(filepath string) bool {
res, _ := this.ShellOne(`echo 1 && [ ! -d "` + filepath + `" ] && echo 2`) res, _ := s.ShellOne(`echo 1 && [ ! -d "` + filepath + `" ] && echo 2`)
if res == "1" { if res == "1" {
return true return true
} else { } else {
@ -514,8 +514,8 @@ func (this *StarSSH) IsFolder(filepath string) bool {
} }
} }
func (this *StarSSH) ShellOneShowScreen(cmd string) (string, error) { func (s *StarSSH) ShellOneShowScreen(cmd string) (string, error) {
newsess, err := this.NewSession() newsess, err := s.NewSession()
if err != nil { if err != nil {
return "", err return "", err
} }
@ -559,8 +559,8 @@ func (this *StarSSH) ShellOneShowScreen(cmd string) (string, error) {
return strings.TrimSpace(string(bytes)), err return strings.TrimSpace(string(bytes)), err
} }
func (this *StarSSH) ShellOneToFunc(cmd string, callback func(string)) (string, error) { func (s *StarSSH) ShellOneToFunc(cmd string, callback func(string)) (string, error) {
newsess, err := this.NewSession() newsess, err := s.NewSession()
if err != nil { if err != nil {
return "", err return "", err
} }

Loading…
Cancel
Save