Skip to content

Instantly share code, notes, and snippets.

@dos65
Created January 14, 2016 20:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dos65/a02d6632b6e5259b0aa8 to your computer and use it in GitHub Desktop.
Save dos65/a02d6632b6e5259b0aa8 to your computer and use it in GitHub Desktop.
package main
import (
"github.com/pkg/sftp"
"golang.org/x/crypto/ssh"
"log"
"io"
)
func main() {
sshConfig := &ssh.ClientConfig{
User: myuser,
Auth: []ssh.AuthMethod{ssh.Password(mypassword)}}
connection, err := ssh.Dial("tcp", host:port, sshConfig)
if err != nil {
log.Fatalf("Failed to dial: %s", err)
}
client, err := sftp.NewClient(connection)
if err != nil {
log.Fatalf("Failed to create client: %s", err)
}
file, err := client.Open(path_to_file)
if err != nil {
log.Fatalf("Failed to open remote file: %s", err)
}
buffer := make([]byte, 512)
for {
n, err := file.Read(buffer)
if err != nil {
if err == io.EOF {
log.Println("EOF")
break
}
log.Fatal(err)
}
log.Print(string(buffer[:n]))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment