Skip to content

Instantly share code, notes, and snippets.

@leehambley
Forked from jedy/go_scp.go
Last active August 29, 2015 14:23
Show Gist options
  • Save leehambley/0295b7d36d367476f017 to your computer and use it in GitHub Desktop.
Save leehambley/0295b7d36d367476f017 to your computer and use it in GitHub Desktop.
// https://blogs.oracle.com/janp/entry/how_the_scp_protocol_works
package main
import (
"fmt"
"golang.org/x/crypto/ssh"
)
const privateKey = `content of id_rsa`
func main() {
signer, _ := ssh.ParsePrivateKey([]byte(privateKey))
clientConfig := &ssh.ClientConfig{
User: "jedy",
Auth: []ssh.AuthMethod{
ssh.PublicKeys(signer),
},
}
client, err := ssh.Dial("tcp", "127.0.0.1:22", clientConfig)
if err != nil {
panic("Failed to dial: " + err.Error())
}
session, err := client.NewSession()
if err != nil {
panic("Failed to create session: " + err.Error())
}
defer session.Close()
go func() {
w, _ := session.StdinPipe()
defer w.Close()
// https://golang.org/pkg/archive/tar/
bytesFromZipWriterWhichShouldBeATarWriter := "123456789\n"
fmt.Fprint(w, bytesFromZipWriterWhichShouldBeATarWriter)
fmt.Fprintf(w, io.EOF)
}()
if err := session.Run("/usr/bin/tar -x - ./"); err != nil {
panic("Failed to run: " + err.Error())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment