• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang sftp.NewClient函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中github.com/pkg/sftp.NewClient函数的典型用法代码示例。如果您正苦于以下问题:Golang NewClient函数的具体用法?Golang NewClient怎么用?Golang NewClient使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了NewClient函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。

示例1: UploadScript

func (i *blueBoxInstance) UploadScript(ctx gocontext.Context, script []byte) error {
	client, err := i.sshClient(ctx)
	if err != nil {
		return err
	}
	defer client.Close()

	sftp, err := sftp.NewClient(client)
	if err != nil {
		return err
	}
	defer sftp.Close()

	_, err = sftp.Lstat("build.sh")
	if err == nil {
		return ErrStaleVM
	}

	f, err := sftp.Create("build.sh")
	if err != nil {
		return err
	}

	_, err = f.Write(script)
	if err != nil {
		return err
	}

	return err
}
开发者ID:henrikhodne-abandoned,项目名称:worker,代码行数:30,代码来源:bluebox.go


示例2: getSftpClient

func getSftpClient(conf Config) []*sftp.Client {
	// process the keyfile
	buf, err := ioutil.ReadFile(conf.KeyFile)
	if err != nil {
		log.Fatalf("error in reading private key file %s\n", err)
	}
	key, err := ssh.ParsePrivateKey(buf)
	if err != nil {
		log.Fatalf("error in parsing private key %s\n", key)
	}
	// client config
	config := &ssh.ClientConfig{
		User: conf.User,
		Auth: []ssh.AuthMethod{ssh.PublicKeys(key)},
	}
	// connection
	clients := make([]*sftp.Client, 0)
	for _, r := range conf.Remotes {
		c, err := ssh.Dial("tcp", r, config)
		if err != nil {
			log.Fatalf("error in ssh connection %s\n", err)
		}
		// sftp handler
		sftp, err := sftp.NewClient(c)
		if err != nil {
			log.Fatalf("error in sftp connection %s\n", err)
		}
		clients = append(clients, sftp)
	}
	return clients
}
开发者ID:dictyBase,项目名称:webhooks,代码行数:31,代码来源:push.go


示例3: copyFile

func (c *DockerDeployClient) copyFile(source string, target string) error {
	sftp, err := sftp.NewClient(c.sshClient)
	if err != nil {
		log.Fatalf("Could not initialize SFTP connection: %v", err)
	}
	defer sftp.Close()

	tf, err := sftp.Create(target)
	if err != nil {
		return err
	}
	defer tf.Close()

	sf, err := os.Open(source)
	if err != nil {
		return err
	}
	defer sf.Close()

	n, err := io.Copy(tf, sf)
	if err != nil {
		return err
	}

	log.Printf("Artifact from %v to %v copied. %v Bytes transferred.", source, target, n)
	return nil
}
开发者ID:herzog31,项目名称:docker-deploy-bamboo-go,代码行数:27,代码来源:client.go


示例4: Connect

func (sfs *SftpFileSystem) Connect() error {
	var auths []ssh.AuthMethod
	if aconn, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK")); err == nil {
		auths = append(auths, ssh.PublicKeysCallback(agent.NewClient(aconn).Signers))
	}
	if len(sfs.sp.Password) != 0 {
		auths = append(auths, ssh.Password(sfs.sp.Password))
	}

	config := ssh.ClientConfig{
		User: sfs.sp.Username,
		Auth: auths,
	}
	var err error
	sfs.sshClient, err = ssh.Dial("tcp", sfs.sp.Server, &config)
	if err != nil {
		log.Println("unable to connect to [%s]: %v", sfs.sp.Server, err)
		return err
	}

	sfs.sftpClient, err = sftp.NewClient(sfs.sshClient)
	if err != nil {
		log.Println("unable to start sftp subsytem: %v", err)
		return err
	}
	return nil
}
开发者ID:peernode,项目名称:Taipei-Torrent,代码行数:27,代码来源:sftp.go


示例5: Connect

func (c *SftpClient) Connect() error {
	auth := []ssh.AuthMethod{}
	if c.authMethod == "key" {
		key, _ := c.GetKey(c.keyPath)

		auth = []ssh.AuthMethod{
			ssh.PublicKeys(key),
		}
	} else if c.authMethod == "password" {
		auth = []ssh.AuthMethod{
			ssh.Password(c.password),
		}
	}
	config := &ssh.ClientConfig{
		User: c.username,
		Auth: auth,
	}
	sHost := strings.Join([]string{c.hostname, strconv.FormatInt(c.port, 10)}, ":")

	sshClient, err := ssh.Dial("tcp", sHost, config)
	if err != nil {
		return err
	}
	sftpClient, err := sftp.NewClient(sshClient)
	if err == nil {
		c.Client = sftpClient
	}
	return err
}
开发者ID:VukDukic,项目名称:gotilla,代码行数:29,代码来源:sftputil.go


示例6: New

// New returns a new SFTP remote Cache implementated.
func New(server, username, password, key string) (cache.Cache, error) {
	config := &ssh.ClientConfig{
		Timeout: time.Minute * 5,
		User:    username,
		Auth: []ssh.AuthMethod{
			ssh.Password(password),
		},
	}

	// private key authentication takes precedence
	if key != "" {
		signer, err := ssh.ParsePrivateKey([]byte(key))
		if err != nil {
			return nil, err
		}
		config.Auth[0] = ssh.PublicKeys(signer)
	}

	// create the ssh connection and client
	client, err := ssh.Dial("tcp", server, config)
	if err != nil {
		return nil, err
	}

	// open the sftp session using the ssh connection
	sftp, err := sftp.NewClient(client)
	if err != nil {
		client.Close()
		return nil, err
	}

	return &cacher{sftp, client}, nil
}
开发者ID:ZombieHippie,项目名称:drone-sftp-cache,代码行数:34,代码来源:sftp.go


示例7: Sftp

// Sftp uploads sourceFile to remote machine
func (ssh_conf *MakeConfig) Sftp(src, dst string) error {
	session, err := ssh_conf.connect()
	if err != nil {
		ssh_conf.close()
		return err
	}
	defer session.Close()

	ssh_conf.SftpClient, err = sftp.NewClient(ssh_conf.Client)
	if err != nil {
		return err
	}
	defer func() {
		ssh_conf.SftpClient.Close()
		ssh_conf.SftpClient = nil
	}()

	sf, err := os.Open(src)
	if err != nil {
		return err
	}
	defer sf.Close()

	f, err := ssh_conf.SftpClient.Create(dst)
	if err != nil {
		return err
	}
	defer f.Close()

	if _, err := io.Copy(f, sf); err != nil {
		return err
	}

	return nil
}
开发者ID:swan-go,项目名称:easyssh,代码行数:36,代码来源:easyssh.go


示例8: FS

//FS creates a ftp client on the remote connection and allows file base op
func (h *SSHProtocolLink) FS() flux.ActionStackInterface {
	req := flux.NewAction()
	eo := flux.NewAction()
	st := flux.NewActionStackBy(req, eo)

	ch := req.Chain(2)

	ch.OverrideBefore(1, func(b interface{}, next flux.ActionInterface) {
		ce, ok := b.(*sftp.Client)

		if !ok {
			return
		}

		ce.Close()
	})

	cl, err := sftp.NewClient(h.conn)

	if err != nil {
		st.Complete(err)
	} else {
		st.Complete(cl)
	}

	return st
}
开发者ID:influx6,项目名称:goproj,代码行数:28,代码来源:sshlink.go


示例9: newClient

func (p *SSHPath) newClient() (*sftp.Client, error) {
	if !p.sudo {
		sftpClient, err := sftp.NewClient(p.client)
		if err != nil {
			return nil, fmt.Errorf("error creating sftp client: %v", err)
		}

		return sftpClient, nil
	} else {
		s, err := p.client.NewSession()
		if err != nil {
			return nil, fmt.Errorf("error creating sftp client (in new-session): %v", err)
		}

		//if err := s.R("sftp"); err != nil {
		//	return nil, fmt.Errorf("error creating sftp client (in new-session): %v", err)
		//}
		stdin, err := s.StdinPipe()
		if err != nil {
			return nil, fmt.Errorf("error creating sftp client (at stdin pipe): %v", err)
		}
		stdout, err := s.StdoutPipe()
		if err != nil {
			return nil, fmt.Errorf("error creating sftp client (at stdout pipe): %v", err)
		}

		err = s.Start("sudo /usr/lib/openssh/sftp-server")
		if err != nil {
			return nil, fmt.Errorf("error creating sftp client (executing 'sudo /usr/lib/openssh/sftp-server'): %v", err)
		}

		return sftp.NewClientPipe(stdout, stdin)
	}

}
开发者ID:crohling,项目名称:kops,代码行数:35,代码来源:sshfs.go


示例10: UploadFile

// UploadFile uploads a local file to the given location on the server.
func (s *Server) UploadFile(source string, dest string) (err error) {
	sshClient, err := s.SSHClient()
	if err != nil {
		return
	}

	client, err := sftp.NewClient(sshClient)
	if err != nil {
		return
	}
	defer client.Close()

	// create all parent dirs of dest
	err = s.MkDir(dest)
	if err != nil {
		return
	}

	// open source, create dest
	sourceFile, err := os.Open(source)
	if err != nil {
		return
	}
	defer sourceFile.Close()

	destFile, err := client.Create(dest)
	if err != nil {
		return
	}

	// copy the file content over
	_, err = io.Copy(destFile, sourceFile)
	return
}
开发者ID:sb10,项目名称:vrpipe,代码行数:35,代码来源:cloud.go


示例11: CreateFile

// CreateFile creates a new file with the given content on the server.
func (s *Server) CreateFile(content string, dest string) (err error) {
	sshClient, err := s.SSHClient()
	if err != nil {
		return
	}

	client, err := sftp.NewClient(sshClient)
	if err != nil {
		return
	}
	defer client.Close()

	// create all parent dirs of dest
	err = s.MkDir(dest)
	if err != nil {
		return
	}

	// create dest
	destFile, err := client.Create(dest)
	if err != nil {
		return
	}

	// write the content
	_, err = io.WriteString(destFile, content)
	return
}
开发者ID:sb10,项目名称:vrpipe,代码行数:29,代码来源:cloud.go


示例12: CopyFile

func (ssc *SSHConfig) CopyFile(local_path, remote_path string) (err tree_lib.TreeError) {
	var (
		sft       *sftp.Client
		f         *sftp.File
		file_data []byte
	)
	err.From = tree_lib.FROM_SSH_COPY_FILE
	sft, err.Err = sftp.NewClient(ssc.conn)
	if !err.IsNull() {
		return
	}
	defer sft.Close()

	file_data, err.Err = ioutil.ReadFile(local_path)
	if !err.IsNull() {
		return
	}

	f, err.Err = sft.Create(remote_path)
	if !err.IsNull() {
		return
	}

	_, err.Err = f.Write(file_data)
	f.Close()
	sft.Close()
	return
}
开发者ID:treescale,项目名称:treescale,代码行数:28,代码来源:ssh.go


示例13: Rollback

func Rollback(rollback int, deploy_config setup.DeployConfiguration, commands setup.CommandConfig) {
	/* remove the current directory, and switch back to version current - number
	 */
	connection, err := NewConnection(deploy_config.TargetHost, deploy_config.TargetPort, deploy_config.Username, deploy_config.Password, deploy_config.PublicKeyPath)
	if err != nil {
		log.Panic(err)
	}
	sftp, err := sftp.NewClient(connection)
	if err != nil {
		log.Fatal(err)
	}
	defer sftp.Close()

	var build_list []string

	w, err := sftp.ReadDir(deploy_config.TargetDirectory)
	for _, folder := range w {
		if folder.Name() == "current" || !folder.IsDir() {
			continue
		}
		build_list = append(build_list, folder.Name())
	}
	sort.Sort(ByDate(build_list))

	rollback_version := build_list[len(build_list)-rollback] //list is ordered old to new, so we want to index in relation to the end of the list.

	//Create a symbolic link to the new directory.
	cmd := fmt.Sprintf("unlink %s", deploy_config.TargetDirectory+"/current")
	ExecuteCmd(connection, cmd, nil, nil)

	cmd = fmt.Sprintf("ln -s -f %s %s", deploy_config.TargetDirectory+"/"+rollback_version, deploy_config.TargetDirectory+"/current")
	ExecuteCmd(connection, cmd, nil, nil)
}
开发者ID:newxan,项目名称:gopistrano,代码行数:33,代码来源:control.go


示例14: Example

func Example(conn *ssh.ClientConn) {
	// open an SFTP sesison over an existing ssh connection.
	sftp, err := sftp.NewClient(conn)
	if err != nil {
		log.Fatal(err)
	}
	defer sftp.Close()

	// walk a directory
	w := sftp.Walk("/home/user")
	for w.Step() {
		if w.Err() != nil {
			continue
		}
		log.Println(w.Path())
	}

	// leave your mark
	f, err := sftp.Create("hello.txt")
	if err != nil {
		log.Fatal(err)
	}
	if _, err := f.Write([]byte("Hello world!")); err != nil {
		log.Fatal(err)
	}

	// check it's there
	fi, err := sftp.Lstat("hello.txt")
	if err != nil {
		log.Fatal(err)
	}
	log.Println(fi)
}
开发者ID:kr,项目名称:sftp,代码行数:33,代码来源:example_test.go


示例15: Put

func (c *Communicator) Put(local string, remote string) error {
	client, err := c.Client()
	if err != nil {
		return err
	}

	sc, err := sftp.NewClient(client)
	if err != nil {
		return err
	}
	defer sc.Close()

	b, err := ioutil.ReadFile(local)
	if err != nil {
		return err
	}

	fo, err := sc.Create(remote)
	if err != nil {
		return err
	}
	defer fo.Close()

	_, err = fo.Write(b)
	if err != nil {
		return err
	}

	return nil
}
开发者ID:kohkimakimoto,项目名称:gluassh,代码行数:30,代码来源:communicator.go


示例16: New

func New(snapshot map[string]time.Time, ctx context.Context, config *Config, remoteRoot string) (Poller, error) {
	sshConn, err := ssh.Dial("tcp", config.HostPort, config.Ssh)
	if err != nil {
		glog.Fatalf("cannot ssh.Dial: %s", err)
		return nil, err
	}

	client, err := sftp.NewClient(sshConn)
	if err != nil {
		sshConn.Close()
		glog.Fatalf("cannot create sftp client: %s", err)
		return nil, err
	}

	p := &poller{
		root:        remoteRoot,
		config:      config,
		conn:        sshConn,
		client:      client,
		updatesChan: make(chan *Update),
		snapshot:    snapshot,
		resumes:     make(map[string]int64),
	}

	go p.loop(ctx)
	glog.V(2).Infoln("poller: started")

	return p, nil
}
开发者ID:a-palchikov,项目名称:poller,代码行数:29,代码来源:poller.go


示例17: DownLoadDirectoryRecurrsively

func DownLoadDirectoryRecurrsively(hostAndPort string, username string,
	password string, remoteSourceDirectory string, localTargetDirectory string) error {

	remoteSourceDirectoryLength := len(remoteSourceDirectory)

	authMethodSlice := make([]ssh.AuthMethod, 0)
	authMethodSlice = append(authMethodSlice, ssh.Password(password))

	clientConfig := ssh.ClientConfig{
		User: username,
		Auth: authMethodSlice,
	}
	connection, err := ssh.Dial("tcp", hostAndPort, &clientConfig)
	if err != nil {
		return err
	}
	defer connection.Close()

	// open an SFTP session over an existing ssh connection.
	client, err := sftp.NewClient(connection)
	if err != nil {
		return err
	}
	defer client.Close()

	// walk a directory
	walk := client.Walk(remoteSourceDirectory)
	for walk.Step() {
		if err := walk.Err(); err != nil {
			return err
		}

		if walk.Stat().IsDir() {
			directoryPath := localTargetDirectory + walk.Path()[remoteSourceDirectoryLength:]
			if err := os.MkdirAll(directoryPath, os.ModePerm); err != nil {
				return err
			}
		} else {
			filePath := localTargetDirectory + walk.Path()[remoteSourceDirectoryLength:]
			file, err := client.Open(walk.Path())
			if err != nil {
				return err
			}
			defer file.Close()

			outputFile, err := os.OpenFile(filePath, os.O_CREATE|os.O_WRONLY, os.ModePerm)
			if err != nil {
				return err
			}
			defer outputFile.Close()

			_, err = file.WriteTo(outputFile)
			if err != nil {
				return err
			}
		}
	}

	return nil
}
开发者ID:cloudawan,项目名称:cloudone_utility,代码行数:60,代码来源:sftp_client.go


示例18: Get

func (c *Communicator) Get(remote string, local string) error {
	client, err := c.Client()
	if err != nil {
		return err
	}

	sc, err := sftp.NewClient(client)
	if err != nil {
		return err
	}
	defer sc.Close()

	fi, err := sc.Open(remote)
	if err != nil {
		return err
	}
	defer fi.Close()

	fo, err := os.Create(local)
	if err != nil {
		return err
	}
	defer fo.Close()

	_, err = io.Copy(fo, fi)
	if err != nil {
		return err
	}

	return nil
}
开发者ID:kohkimakimoto,项目名称:gluassh,代码行数:31,代码来源:communicator.go


示例19: NewSFTPClient

func NewSFTPClient(addr string, config *ssh.ClientConfig) (*sftp.Client, error) {
	conn, err := ssh.Dial("tcp", addr, config)
	if err != nil {
		return nil, err
	}

	return sftp.NewClient(conn)
}
开发者ID:iwat,项目名称:sftpsync,代码行数:8,代码来源:remote.go


示例20: UploadFile

func (c *sshClientImpl) UploadFile(username string, password string, ip string, srcFile string, destFile string) error {
	config := &myssh.ClientConfig{
		User: username,
		Auth: []myssh.AuthMethod{
			myssh.Password(password),
		},
	}
	if !IsIP(ip) {
		return errors.New("invalid IP address")
	}

	if IsDir(srcFile) || IsDir(destFile) {
		return errors.New("Is a directory")
	}

	client, err := myssh.Dial("tcp", ip+":22", config)
	if err != nil {
		log.Fatal(err)
		return err
	}
	defer client.Close()

	sftp, err := sftp.NewClient(client)
	if err != nil {
		log.Fatal(err)
		return err
	}
	defer sftp.Close()

	data, err := ioutil.ReadFile(srcFile)
	if err != nil {
		log.Fatal(err)
		return err
	}

	f, err := sftp.Create(destFile)
	if err != nil {
		log.Fatal(err)
		return err
	}
	defer f.Close()

	_, err = f.Write([]byte(data))
	if err != nil {
		log.Fatal(err)
		return err
	}

	_, err = sftp.Lstat(destFile)
	if err != nil {
		log.Fatal(err)
		return err
	}
	return nil
}
开发者ID:ardnaxelarak,项目名称:bosh-softlayer-cpi,代码行数:55,代码来源:ssh_client.go



注:本文中的github.com/pkg/sftp.NewClient函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang sftp.Client类代码示例发布时间:2022-05-28
下一篇:
Golang sftp.Create函数代码示例发布时间:2022-05-28
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap