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

Golang sftp.Close函数代码示例

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

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



在下文中一共展示了Close函数的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: Put

func (conn *SshConfig) Put(local, remote string) error {
	sftp, err := conn.transport()
	if err != nil {
		return err
	}

	defer sftp.Close()

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

	dst, err := sftp.Create(remote)
	if err != nil {
		return errors.New("Could not create the remote file.")
	}

	fmt.Printf("%s --> %s:%s\n", local, conn.Name, remote)
	if _, err := dst.Write(src); err != nil {
		return err
	}

	return nil
}
开发者ID:kbdhero,项目名称:cogs,代码行数:25,代码来源:ftp.go


示例3: Create

// Create creates all the necessary files and directories for a new sftp
// backend at dir. Afterwards a new config blob should be created. `dir` must
// be delimited by forward slashes ("/"), which is required by sftp.
func Create(dir string, program string, args ...string) (*SFTP, error) {
	debug.Log("%v %v", program, args)
	sftp, err := startClient(program, args...)
	if err != nil {
		return nil, err
	}

	// test if config file already exists
	_, err = sftp.c.Lstat(Join(dir, backend.Paths.Config))
	if err == nil {
		return nil, errors.New("config file already exists")
	}

	// create paths for data, refs and temp blobs
	for _, d := range paths(dir) {
		err = sftp.mkdirAll(d, backend.Modes.Dir)
		if err != nil {
			return nil, err
		}
	}

	err = sftp.Close()
	if err != nil {
		return nil, errors.Wrap(err, "Close")
	}

	// open backend
	return Open(dir, program, args...)
}
开发者ID:ckemper67,项目名称:restic,代码行数:32,代码来源:sftp.go


示例4: 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


示例5: 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


示例6: 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


示例7: 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


示例8: create_version

/*
	Create a new version, remove oldest if there are more than *Rollback
	return the new version number so that the deploy knows where to place shit.
*/
func create_version(deploy_config setup.DeployConfiguration) string {
	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()

	new_version := time.Now().Format("20060102150405")

	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))

	//Remove oldest in build_list
	if len(build_list)+1 > deploy_config.Rollbacks+1 {
		remove_list := build_list[:len(build_list)-deploy_config.Rollbacks] //remove any old that exceeds Rollbaks, which are in the start of the list
		for _, version := range remove_list {
			log.Printf("Removing version: %s\n", deploy_config.TargetDirectory+"/"+version)
			//err :=sftp.Remove(deploy_config.TargetDirectory + "/" + version)
			cmd := fmt.Sprintf("rm -r %s", deploy_config.TargetDirectory+"/"+version)
			err := ExecuteCmd(connection, cmd, nil, nil)
			if err != nil {
				log.Println(err)
			}
		}
	}

	log.Printf("Creating folder: %s\n", deploy_config.TargetDirectory+"/"+new_version)
	sftp.Mkdir(deploy_config.TargetDirectory + "/" + new_version)

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

	cmd = fmt.Sprintf("ln -s -f %s %s", deploy_config.TargetDirectory+"/"+new_version, deploy_config.TargetDirectory+"/current")
	//log.Printf("Creating symlink: %s", cmd)
	ExecuteCmd(connection, cmd, nil, nil)

	return new_version
}
开发者ID:newxan,项目名称:gopistrano,代码行数:56,代码来源:transfer.go


示例9: main

func main() {
	if err := resolveSettings(pars); err != nil {
		log.Fatal(err)
	}

	conn, err := initSSH(pars)
	if err != nil {
		log.Fatal(err)
	}

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

	finStat, err := os.Stat(inPath)
	if err != nil {
		log.Fatal(err)
	}
	origSize := finStat.Size()

	fin, err := os.Open(inPath)
	if err != nil {
		log.Fatal(err)
	}
	defer fin.Close()

	outPath := path.Clean(path.Join(pars.DstFolder, filepath.Base(inPath)))
	fout, err := sftp.Create(outPath)
	if err != nil {
		log.Fatal(err)
	}
	if _, err := io.Copy(fout, fin); err != nil {
		log.Fatal(err)
	}

	foutStat, err := sftp.Lstat(outPath)
	if err != nil {
		log.Fatal(err)
	}
	if finalSize := foutStat.Size(); origSize != finalSize {
		if err := sftp.Remove(outPath); err != nil {
			log.Printf("[ERROR] Unable to remove target file: %v", err)
		}
		log.Fatalf("Failed to upload %s to %s: expected %d bytes, got %d (missing %d)",
			inPath, outPath, origSize, finalSize, origSize-finalSize)
	}

	fmt.Println("Successfully uploaded", outPath)
}
开发者ID:alexaandru,项目名称:up,代码行数:51,代码来源:main.go


示例10: DownloadFile

func (c *sshClientImpl) DownloadFile(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()

	pFile, err := sftp.Open(srcFile)
	if err != nil {
		log.Fatal(err)
		return err
	}
	defer pFile.Close()

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

	err = ioutil.WriteFile(destFile, data, 0755)
	if err != nil {
		log.Fatal(err)
		return err
	}

	return nil
}
开发者ID:ardnaxelarak,项目名称:bosh-softlayer-cpi,代码行数:51,代码来源:ssh_client.go


示例11: deploy_local

func deploy_local(deploy_config setup.DeployConfiguration, commands setup.CommandConfig) {
	connection, err := NewConnection(deploy_config.TargetHost, deploy_config.TargetPort, deploy_config.Username, deploy_config.Password, deploy_config.PublicKeyPath)
	if err != nil {
		log.Panic(err)
	}

	//If this is the first time deploying, we need to create the path to the target directory
	if !CheckTargetDirectory(connection, deploy_config) {
		cmd := fmt.Sprintf("mkdir -p %s", deploy_config.TargetDirectory)
		ExecuteCmd(connection, cmd, nil, nil)
		//cmd = fmt.Sprintf("mkdir -p %s", deploy_config.TargetDirectory+"/current")
		//ExecuteCmd(connection, cmd, nil, nil)
	}

	new_version := create_version(deploy_config)

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

	files := list_local_directory(deploy_config.Ignores)

	//TODO use "current" instead of the new_version directory, since we have a symbolic link
	for _, file := range files {
		content, _ := ioutil.ReadFile(file)
		file = strings.Replace(file, `\`, `/`, -1)
		target := deploy_config.TargetDirectory + "/" + new_version + "/" + file
		tree := deploy_config.TargetDirectory + "/" + new_version + "/"

		for _, dir := range strings.Split(file, "/")[:len(strings.Split(file, "/"))-1] {
			tree += dir + "/"
			log.Printf("Creating directory: %s\n", tree)
			err := sftp.Mkdir(tree)
			if err != nil {

			}
		}
		log.Printf("Creating file: %s\n", target)
		f, err := sftp.Create(target)
		if err != nil {
			log.Panic(err)
		}
		if _, err := f.Write([]byte(content)); err != nil {
			log.Panic(err)
		}
	}
}
开发者ID:newxan,项目名称:gopistrano,代码行数:49,代码来源:transfer.go


示例12: ReadAll

// Read a remote file and return the contents.
func (c *Client) ReadAll(filepath string) ([]byte, error) {
	sftp, err := c.SFTPClient()
	if err != nil {
		panic(err)
	}
	defer sftp.Close()

	file, err := sftp.Open(filepath)
	if err != nil {
		return nil, err
	}
	defer file.Close()

	return ioutil.ReadAll(file)
}
开发者ID:bvnarayan,项目名称:simplessh,代码行数:16,代码来源:simplessh.go


示例13: main

func main() {

	// Attempt to open the input file
	flag.Parse()

	csv, err := os.OpenFile(*input, os.O_RDONLY, 0)
	if err != nil {
		log.Fatalln(err)
	}
	defer csv.Close()
	reader := bufio.NewReader(csv)
	if err != nil {
		log.Fatalln(err)
	}

	// SSH connection.
	config := &ssh.ClientConfig{
		User: *user,
		Auth: []ssh.AuthMethod{
			ssh.Password(*secret),
		},
	}

	conn, err := ssh.Dial("tcp", *address, config)
	if err != nil {
		log.Fatalln(err)
	}
	defer conn.Close()

	// SFTP client.
	sftp, err := sftp.NewClient(conn)
	if err != nil {
		log.Fatalln(err)
	}
	defer sftp.Close()

	// Upload over SFTP.
	sftpFile, err := sftp.OpenFile(filepath.Join(*directory, *file), os.O_WRONLY|os.O_CREATE|os.O_TRUNC)
	if err != nil {
		log.Fatalln(err)
	}
	defer sftpFile.Close()
	sftpFile.ReadFrom(reader)
}
开发者ID:contactlab,项目名称:CLABPush-Go,代码行数:44,代码来源:main.go


示例14: copyToRemote

func copyToRemote(c *cli.Context) {
	for _, flag := range []string{"keyfile", "to", "host", "user"} {
		if c.Generic(flag) == nil {
			log.Fatalf("flag %s is required\n", flag)
		}
	}
	// process the keyfile
	buf, err := ioutil.ReadFile(c.String("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: c.String("user"),
		Auth: []ssh.AuthMethod{ssh.PublicKeys(key)},
	}
	// connection
	client, err := ssh.Dial("tcp", c.String("host")+":22", config)
	if err != nil {
		log.Fatalf("error in ssh connection %s\n", err)
	}
	defer client.Close()
	// sftp handler
	sftp, err := sftp.NewClient(client)
	if err != nil {
		log.Fatalf("error in sftp connection %s\n", err)
	}
	defer sftp.Close()
	// Remote file
	r, err := sftp.Create(filepath.Join(c.String("to"), "go_sftp.txt"))
	if err != nil {
		log.Fatalf("error in creating remote file %s\n", err)
	}
	l := strings.NewReader("Writing through golang sftp")
	if _, err := io.Copy(r, l); err != nil {
		log.Fatalf("error in writing file to remote system %s\n", err)
	}
	log.Println("written new file to remote system")
}
开发者ID:dictyBase,项目名称:webhooks,代码行数:43,代码来源:scp.go


示例15: UploadScript

func (i *jupiterBrainInstance) UploadScript(ctx context.Context, script []byte) error {
	client, err := i.sshClient()
	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
	}

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

	_, err = fmt.Fprintf(f, wrapperSh)

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


示例16:

			Expect(err).NotTo(HaveOccurred())
			defer session.Close()

			accepted, err := session.SendRequest("subsystem", true, ssh.Marshal(subsysMsg{Subsystem: "sftp"}))
			Expect(err).NotTo(HaveOccurred())
			Expect(accepted).To(BeTrue())
		})

		It("starts an sftp server in write mode", func() {
			tempDir, err := ioutil.TempDir("", "sftp")
			Expect(err).NotTo(HaveOccurred())
			defer os.RemoveAll(tempDir)

			sftp, err := sftp.NewClient(client)
			Expect(err).NotTo(HaveOccurred())
			defer sftp.Close()

			By("creating the file")
			target := filepath.Join(tempDir, "textfile.txt")
			file, err := sftp.Create(target)
			Expect(err).NotTo(HaveOccurred())

			fileContents := []byte("---\nthis is a simple file\n\n")
			_, err = file.Write(fileContents)
			Expect(err).NotTo(HaveOccurred())

			err = file.Close()
			Expect(err).NotTo(HaveOccurred())

			Expect(ioutil.ReadFile(target)).To(Equal(fileContents))
开发者ID:benjaminharnett,项目名称:diego-ssh,代码行数:30,代码来源:session_channel_handler_test.go


示例17: Get

func (conn *SshConfig) Get(rPath string, env *Enviroment) error {
	sftp, err := conn.transport()
	if err != nil {
		return err
	}

	defer sftp.Close()

	// handler for *glob in rpath
	if GlobIn(PathChomp(rPath)) {
		err := conn.GlobHandler(rPath, sftp, env)
		if err != nil {
			return err
		}

		return nil
	}

	remoteFile, err := sftp.Open(rPath)
	if err != nil {
		return err
	}

	fStat, err := remoteFile.Stat()
	if err != nil {
		return err
	}

	if _, err := os.Stat(conn.Name); os.IsNotExist(err) {
		os.Mkdir(conn.Name, 0755)
	}

	switch {

	case (!IsRegularFile(fStat)):
		w := sftp.Walk(rPath)
		for w.Step() {
			if w.Err() != nil {
				continue
			}

			if IsRegularFile(w.Stat()) {
				err := conn.GetFile(w.Path(), PrependParent(conn.Name, w.Path()), sftp, env)
				if err != nil {
					return err
				}

			} else {
				_, err := os.Stat(PrependParent(conn.Name, w.Path()))

				if os.IsNotExist(err) {
					os.Mkdir(PrependParent(conn.Name, w.Path()), 0755)
				}
			}
		}

	default:
		conn.GetFile(rPath, filepath.Join(conn.Name, PathChomp(rPath)), sftp, env)
	}

	return nil
}
开发者ID:kbdhero,项目名称:cogs,代码行数:62,代码来源:ftp.go


示例18: Handle

func (r *Resource) Handle() {
	Log := log.New(os.Stdout, fmt.Sprintf("%s[%d] ", r.Parent.URL, r.DeviceID), log.Ltime|log.Lshortfile)

	for {
		time.Sleep(1 * time.Second)

		if !r.Parent.Enabled || r.InUse {
			continue
		}

		// Get Job
		var jobInstance *JobInstance = nil
		select {
		case r := <-r.JobQueue:
			jobInstance = r
		default:

		}

		if jobInstance == nil {
			select {
			case a := <-JobQueue:
				jobInstance = a
			default:
			}
		}

		if jobInstance == nil {
			continue
		}
		r.InUse = true

		Log.Println(jobInstance)

		if jobInstance.PID == -1 {
			// Send Model Data
			Log.Println("Uploading Model")
			sftp, err := sftp.NewClient(r.Parent.Client)
			if err != nil {
				Log.Fatal(err)
			}

			sftp.Mkdir(sftp.Join(r.Parent.WorkingDirectory, "model"))
			sftp.Mkdir(sftp.Join(r.Parent.WorkingDirectory, "model", strings.ToLower(jobInstance.Parent.Model.Name)))

			for _, file := range jobInstance.Parent.Model.Files {
				fIn, err := os.Open(fmt.Sprintf("data/%s/%s", jobInstance.Parent.Model.Name, file))
				if err != nil {
					Log.Fatalln(err)
				}

				fOut, err := sftp.Create(sftp.Join(r.Parent.WorkingDirectory, "model", strings.ToLower(jobInstance.Parent.Model.Name), file))
				if err != nil {
					Log.Fatalln(err)
				}

				io.Copy(fOut, fIn)

				fIn.Close()
				fIn.Close()
			}

			// Setup Job Type Parameters
			executable := ""
			configuration := ""

			if jobInstance.Parent.Template.IsProtoMol() {
				executable = "ProtoMol"
				configuration = "sim.conf"
			} else {
				executable = "python"
				configuration = "sim.py"
			}

			// Send Template Data
			template, err := jobInstance.Parent.Template.Process(r.DeviceID, *jobInstance.Parent)
			if err != nil {
				Log.Fatalln(err)
			}

			sftp.Mkdir(sftp.Join(r.Parent.WorkingDirectory, "job"))
			sftp.Mkdir(sftp.Join(r.Parent.WorkingDirectory, "job", strings.ToLower(jobInstance.Parent.Name)))
			sftp.Mkdir(sftp.Join(r.Parent.WorkingDirectory, "job", strings.ToLower(jobInstance.Parent.Name), strings.ToLower(fmt.Sprintf("%d", jobInstance.NumberInSequence()))))

			fOut, err := sftp.Create(sftp.Join(r.Parent.WorkingDirectory, "job", strings.ToLower(jobInstance.Parent.Name), strings.ToLower(fmt.Sprintf("%d", jobInstance.NumberInSequence())), configuration))
			if err != nil {
				Log.Fatalln(err)
			}
			fOut.Write(template)
			fOut.Close()

			sftp.Close()

			// Start job and retrieve PID
			Log.Println("Starting Job")
			session, err := r.Parent.Client.NewSession()
			if err != nil {
				Log.Fatalln(err)
			}

//.........这里部分代码省略.........
开发者ID:LCLS,项目名称:GPUManager,代码行数:101,代码来源:server_resource.go


示例19: Put

// source is the source of the file/folder
// destination is the path of the FOLDER where source will be transferred to
// NOTE: source will keep it original name when transferred
func (sshTransport *SSHTransport) Put(source, destination string, srcType string) error {
	client, session, err := sshTransport.getClientSession()
	if err != nil {
		return HenchErr(err, map[string]interface{}{
			"host": sshTransport.Host,
		}, fmt.Sprintf("Couldn't dial into %s", sshTransport.Host))
	}
	defer client.Close()
	defer session.Close()

	sftp, err := sftp.NewClient(client)
	if err != nil {
		return HenchErr(err, map[string]interface{}{
			"host": sshTransport.Host,
		}, "Error creating sftp client")
	}
	defer sftp.Close()

	dstPieces := strings.Split(destination, "/")

	// it will always equal home
	if dstPieces[0] == "${HOME}" {
		dstPieces[0] = "."
	}

	newDst := strings.Join(dstPieces, "/")

	// if the src is a dir
	// tar the dir, copy it over, untar it
	// remove tar files on both ends
	if srcType == "dir" {
		// This is done so hosts won't remove tars used by other file
		tarredFile := sshTransport.Host + "_" + filepath.Base(source) + ".tar"

		// create the local tar
		err := tarit(source, tarredFile, nil)

		if err != nil {
			return HenchErr(err, map[string]interface{}{
				"dir": source,
			}, "Failed to tar dir to transport")
		}
		defer os.Remove(tarredFile)

		sourceBuf, err := ioutil.ReadFile(tarredFile)
		if err != nil {
			return HenchErr(err, map[string]interface{}{
				"dir":  source,
				"file": tarredFile,
			}, "Failed to read tar")
		}

		tarredFilePath := filepath.Join(newDst, tarredFile)
		f, err := sftp.Create(tarredFilePath)
		if err != nil {
			return HenchErr(err, map[string]interface{}{
				"host": sshTransport.Host,
				"file": newDst,
			}, "Failed to create remote file")
		}

		if _, err := f.Write(sourceBuf); err != nil {
			return HenchErr(err, map[string]interface{}{
				"host":   sshTransport.Host,
				"file":   destination,
				"source": source,
			}, "Error writing to remote file ")
		}

		cmd := fmt.Sprintf("tar -xvf %s -C %s && rm -rf %s", tarredFilePath, newDst, tarredFilePath)
		_, err = session.CombinedOutput(cmd)
		if err != nil {
			return HenchErr(err, map[string]interface{}{
				"host": sshTransport.Host,
				"file": tarredFilePath,
			}, "While untarring on remote")
		}
	} else {
		f, err := sftp.Create(filepath.Join(newDst, filepath.Base(source)))
		if err != nil {
			return HenchErr(err, map[string]interface{}{
				"host": sshTransport.Host,
				"file": destination,
			}, "Failed to create remote file")
		}

		sourceBuf, err := ioutil.ReadFile(source)
		if err != nil {
			return HenchErr(err, map[string]interface{}{
				"file": source,
			}, "")
		}

		if _, err := f.Write(sourceBuf); err != nil {
			return HenchErr(err, map[string]interface{}{
				"host":   sshTransport.Host,
				"file":   destination,
//.........这里部分代码省略.........
开发者ID:apigee,项目名称:henchman,代码行数:101,代码来源:ssh.go


示例20: Synchronise

func Synchronise(ssh *ssh.Client, supervised, pushOnly bool, localDir, remoteDir string) {
	/*
		Algorithm:
		- Get all local files and dirs
		- Get all remote files and dirs
		- Determine: ndl := newest mod time of all local files
		- Determine: ndr := newest mod time of all remote files
		- Free remote space
			- Are remote files present, which are locally not present
				and these remote files are older than ndl?
					- Meaning: You have worked locally and remotely are older files which are not locally present.
							   Interpretation of change: You have deleted these files locally, because you own local
							   files which are newer that these files.
					- delete these remote files! (may ask the user)
		- Free local space
			- Are local files present, which are remotely not present
				and these local files are older than ndr?
					- Meaning: You have worked remotely (or have synced with another PC), thus, local files are older
							   and these are not present remotely. Interpretation of change: You have delete these files
							   remotely, because your remotely files are newer that these files.
					- delete these local files! (may ask the user)
		- Download any new files
			- Are remote files present, which are locally not present?
					- Meaning: These files are new to the local side
					- Download these files! (may ask the user)
		- Upload any new files
			- Are local files present, which are remotely not present?
					- Meaning: These files are new to the remote side
					- Upload these files! (may ask the user)
		- Changed remote files
			- Are remote and local files present, where the remote file is newer?
					- Meaning: These files are changed on the remote side
					- Download these files and replace the local copies (may ask the user)
		- Changed local files
			- Are local and  remote files present, where the local file is newer?
					- Meaning: These files are changed on the local side
					- Upload these files and replace the remote copies (may ask the user)
	*/

	//
	// Read the local files ============================================================================================
	//
	log.Println("Try to read all local files now...")
	localFiles = make(map[string]os.FileInfo)
	filepath.Walk(localDir, walkerlocal)
	log.Printf("Found %d local files.\n", len(localFiles))

	//
	// Connect to the server ============================================================================================
	//
	sftp, sftpError := sftp.NewClient(ssh)
	if sftpError != nil {
		log.Println("Was not able to connect to the server: " + sftpError.Error())
		os.Exit(7)
		return
	}

	defer sftp.Close()

	//
	// Read the remote files ============================================================================================
	//
	log.Println("Try to read all remote files now...")
	remoteFiles = make(map[string]os.FileInfo)
	counterRemoteFile := 0
	walker := sftp.Walk(remoteDir)
	for walker.Step() {
		counterRemoteFile++
		if walker.Err() != nil {
			continue
		}

		remoteFiles[walker.Path()] = walker.Stat()
		if counterRemoteFile%512 == 0 {
			fmt.Printf("%05d.\n", counterRemoteFile)
		} else if counterRemoteFile%16 == 0 {
			fmt.Printf("%05d.", counterRemoteFile)
		}
	}

	fmt.Println()
	log.Printf("Found %d remote files.\n", len(remoteFiles))

	//
	// Normalise all local paths ============================================================================================
	//
	localFilesNormalised = make(map[string]os.FileInfo)
	for key, value := range localFiles {
		normalised := normalisePath(localDir, key)
		if normalised != `` {
			localFilesNormalised[normalised] = value
			normalised2localFiles[normalised] = key
		}
	}

	//
	// Normalise all remote paths ============================================================================================
	//
	remoteFilesNormalised = make(map[string]os.FileInfo)
	for key, value := range remoteFiles {
//.........这里部分代码省略.........
开发者ID:SommerEngineering,项目名称:Sync,代码行数:101,代码来源:Synchronise.go



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


鲜花

握手

雷人

路过

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

请发表评论

全部评论

专题导读
上一篇:
Golang sftp.Create函数代码示例发布时间:2022-05-28
下一篇:
Golang profile.Start函数代码示例发布时间: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