Exemplo n.º 1
0
func CopyLocalFileToRemote(client *ssh.Client, localFilePath string, filename string) error {
	// Each ClientConn can support multiple interactive sessions,
	// represented by a Session.
	session, err := client.NewSession()
	if err != nil {
		log.Fatal("Failed to create session: " + err.Error())
	}
	defer session.Close()

	writer, err := session.StdinPipe()
	if err != nil {
		return err
	}
	defer writer.Close()

	go func() {
		fileContents, _ := ioutil.ReadFile(localFilePath + "/" + filename)
		content := string(fileContents)
		fmt.Fprintln(writer, "C0644", len(content), filename)
		fmt.Fprint(writer, content)
		fmt.Fprintln(writer, "\x00") // transfer end with \x00\
	}()

	session.Run("/usr/bin/scp -t ./")
	return nil
}
Exemplo n.º 2
0
func ExecuteCommand(client *ssh.Client, cmd string) (string, error) {
	// Each ClientConn can support multiple interactive sessions,
	// represented by a Session.
	session, err := client.NewSession()
	if err != nil {
		log.Fatal("Failed to create session: " + err.Error())
	}
	defer session.Close()

	// Once a Session is created, you can execute a single command on
	// the remote side using the Run method.
	var b bytes.Buffer
	session.Stdout = &b
	if err := session.Run(cmd); err != nil {
		return "", err
	}

	return b.String(), nil
}
Exemplo n.º 3
0
func CopyRemoteFileToLocal(client *ssh.Client, remoteFilePath string, remoteFilename string, localFilePath string, localFileName string) error {
	// Each ClientConn can support multiple interactive sessions,
	// represented by a Session.
	session, err := client.NewSession()
	if err != nil {
		log.Fatal("Failed to create session: " + err.Error())
	}
	defer session.Close()

	writer, err := session.StdinPipe()
	if err != nil {
		return err
	}

	reader, err := session.StdoutPipe()
	if err != nil {
		return err
	}

	doneChannel := make(chan bool)

	go func(writer io.WriteCloser, reader io.Reader, doneChannel chan bool) {
		successfulByte := []byte{0}

		// Send a null byte saying that we are ready to receive the data
		writer.Write(successfulByte)
		// We want to first receive the command input from remote machine
		// e.g. C0644 113828 test.csv
		scpCommandArray := make([]byte, 100)
		bytes_read, err := reader.Read(scpCommandArray)
		if err != nil {
			if err == io.EOF {
				//no problem.
			} else {
				log.Fatalf("Error reading standard input: %s", err.Error())
			}
		}

		scpStartLine := string(scpCommandArray[:bytes_read])
		scpStartLineArray := strings.Split(scpStartLine, " ")

		filePermission := scpStartLineArray[0][1:]
		fileSize := scpStartLineArray[1]
		fileName := scpStartLineArray[2]

		log.Printf("File with permissions: %s, File Size: %s, File Name: %s", filePermission, fileSize, fileName)

		// Confirm to the remote host that we have received the command line
		writer.Write(successfulByte)
		// Now we want to start receiving the file itself from the remote machine
		fileContents := make([]byte, 1)
		var file *os.File
		if localFileName == "" {
			file = createNewFile(localFilePath + "/" + fileName)
		} else {
			file = createNewFile(localFilePath + "/" + localFileName)
		}
		more := true
		for more {
			bytes_read, err = reader.Read(fileContents)
			if err != nil {
				if err == io.EOF {
					more = false
				} else {
					log.Fatalf("Error reading standard input: %s", err.Error())
				}
			}
			writeParitalToFile(file, fileContents[:bytes_read])
			writer.Write(successfulByte)
		}
		err = file.Sync()
		if err != nil {
			log.Fatal(err)
		}
		doneChannel <- true
	}(writer, reader, doneChannel)

	session.Run("/usr/bin/scp -f " + remoteFilePath + "/" + remoteFilename)
	<-doneChannel
	writer.Close()
	return nil
}