March Update

master
兔子 5 years ago
parent 54cdb80ef7
commit 4a21bb48bf

@ -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
}

Loading…
Cancel
Save