Skip to content

Commit

Permalink
属性权限修改
Browse files Browse the repository at this point in the history
  • Loading branch information
MrYZhou committed Jan 4, 2023
1 parent 3caa618 commit 0dffd1a
Showing 1 changed file with 16 additions and 19 deletions.
35 changes: 16 additions & 19 deletions ssh/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"path"
"strings"

. "github.com/MrYZhou/outil/file"
"github.com/pkg/sftp"
"golang.org/x/crypto/ssh"
)
Expand All @@ -18,8 +17,8 @@ type Cli struct {
user string
password string
host string
client *ssh.Client
sftpClient *sftp.Client
Client *ssh.Client
SftpClient *sftp.Client
LastResult string
}

Expand All @@ -35,19 +34,19 @@ func (c *Cli) Connect() (*Cli, error) {
if nil != err {
return c, err
}
c.client = client
c.sftpClient = sftp
c.Client = client
c.SftpClient = sftp
return c, nil
}

// 执行shell
// 执行shellclient
func (c Cli) Run(shell string) (string, error) {
if c.client == nil {
if c.Client == nil {
if _, err := c.Connect(); err != nil {
return "", err
}
}
session, err := c.client.NewSession()
session, err := c.Client.NewSession()
if err != nil {
return "", err
}
Expand All @@ -56,8 +55,8 @@ func (c Cli) Run(shell string) (string, error) {

r, err := session.StdoutPipe()
if err != nil {
fmt.Println(err)
os.Exit(1001)
fmt.Println(err)
os.Exit(1001)
}
go io.Copy(os.Stdout, r)

Expand All @@ -72,16 +71,14 @@ func Server(host string, user string, password string) *Cli {
host: host,
user: user,
password: password,
sftpClient: nil,
client: nil,
}
c, _ := cli.Connect()
return c
}

// 创建目录
func (c *Cli) createDir(dir string) {
c.sftpClient.MkdirAll(dir)
c.SftpClient.MkdirAll(dir)
}

// 批量创建目录
Expand All @@ -93,7 +90,7 @@ func (c *Cli) createDirList(list []string) {

// 判断文件是否存在
func (c *Cli) IsFileExist(path string) bool {
info, _ := c.sftpClient.Stat(path)
info, _ := c.SftpClient.Stat(path)
if info != nil {
return true
}
Expand All @@ -107,13 +104,13 @@ remoteFileName 文件名
*/
func (c *Cli) CreateFile(remoteFileName string) (*sftp.File, error) {
remoteDir := path.Dir(remoteFileName)
c.sftpClient.MkdirAll(remoteDir)
ftpFile, err := c.sftpClient.Create(remoteFileName)
c.SftpClient.MkdirAll(remoteDir)
ftpFile, err := c.SftpClient.Create(remoteFileName)
return ftpFile, err
}
func initClient(c *Cli) *Cli {

if c.sftpClient == nil {
if c.SftpClient == nil {
cli, _ := c.Connect()
return cli
} else {
Expand All @@ -130,12 +127,12 @@ base 本地文件夹路径
target 远程文件夹路径
*/
func (c *Cli) UploadDir(base string, target string) {
c.sftpClient.MkdirAll(target)
c.SftpClient.MkdirAll(target)
list, dirList := ReadDirAll(base)
// 创建远程目录
for _, f := range dirList {
targetPath := strings.Replace(f, base, target, 1)
c.sftpClient.MkdirAll(targetPath)
c.SftpClient.MkdirAll(targetPath)
}
// 创建远程文件
for _, f := range list {
Expand Down

0 comments on commit 0dffd1a

Please sign in to comment.