|
|
@ -19,7 +19,7 @@ import (
|
|
|
|
var ShellRes, ShellErr string
|
|
|
|
var ShellRes, ShellErr string
|
|
|
|
var ShellExit bool
|
|
|
|
var ShellExit bool
|
|
|
|
|
|
|
|
|
|
|
|
type sshd struct {
|
|
|
|
type Sshd struct {
|
|
|
|
SSHC *ssh.Session
|
|
|
|
SSHC *ssh.Session
|
|
|
|
infile io.Writer
|
|
|
|
infile io.Writer
|
|
|
|
outfile io.Reader
|
|
|
|
outfile io.Reader
|
|
|
@ -152,7 +152,30 @@ func CreateSftp(client *ssh.Client) (*sftp.Client, error) {
|
|
|
|
return sftpClient, err
|
|
|
|
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)
|
|
|
|
srcFile, err := sftpClient.Open(src)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
@ -183,9 +206,9 @@ func Command(session *ssh.Session, cmdstr string) (string, error) {
|
|
|
|
return res.String(), nil
|
|
|
|
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
|
|
|
|
var err error
|
|
|
|
lovessh := sshd{}
|
|
|
|
lovessh := Sshd{}
|
|
|
|
lovessh.SSHC = session
|
|
|
|
lovessh.SSHC = session
|
|
|
|
lovessh.infile, err = lovessh.SSHC.StdinPipe()
|
|
|
|
lovessh.infile, err = lovessh.SSHC.StdinPipe()
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
@ -216,7 +239,7 @@ func SedColor(str string) string {
|
|
|
|
return string(reg.ReplaceAll([]byte(str), []byte("")))
|
|
|
|
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
|
|
|
|
var stop bool
|
|
|
|
reader := bufio.NewReader(this.outfile)
|
|
|
|
reader := bufio.NewReader(this.outfile)
|
|
|
|
erreader := bufio.NewReader(this.errfile)
|
|
|
|
erreader := bufio.NewReader(this.errfile)
|
|
|
@ -271,16 +294,16 @@ func (this sshd) GetResult(maxtime int) (string, string, bool) {
|
|
|
|
return restr, errstr, true
|
|
|
|
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"))
|
|
|
|
this.infile.Write([]byte(cmdstr + "\n"))
|
|
|
|
return this.GetResult(maxtime)
|
|
|
|
return this.GetResult(maxtime)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (this sshd) WriteCmd(cmdstr string) {
|
|
|
|
func (this Sshd) WriteCmd(cmdstr string) {
|
|
|
|
this.infile.Write([]byte(cmdstr + "\n"))
|
|
|
|
this.infile.Write([]byte(cmdstr + "\n"))
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (this sshd) IsExit() bool {
|
|
|
|
func (this Sshd) IsExit() bool {
|
|
|
|
return ShellExit
|
|
|
|
return ShellExit
|
|
|
|
}
|
|
|
|
}
|
|
|
|