当前位置: 首页>>代码示例>>Golang>>正文


Golang sftp.NewClient函数代码示例

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


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


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