2014-04-13 3 views
3

Go에서 3 개월 간의 SCP 구현을 수행했습니다.Golang의 SCP 예제

작동중인 lib 또는 예제를 찾을 수 없습니다.

이 요점은 작동하지 않습니다. 그것은 버그가 있지만 작업 코드로 일부 답변에 나열됩니다. https://gist.github.com/jedy/3357393

+1

당신은 많은 downvotes를 얻을 것이다. '작동하지 않는 것'을 기술하고, 예상했던 것을 말하고, 코드를 질문에 복사하고 (여기에 링크를 넣지 마십시오.) 유용한 답을 얻을 수있는 더 좋은 기회를 가질 수 있습니다. – topskip

답변

1

나는 그것을 사용하지는 않았지만, godoc에서 SCP를 검색하면 scp-go이 나왔습니다.

package main 

import (
    "io/ioutil" 
    "net" 

    "github.com/tmc/scp" 
    "golang.org/x/crypto/ssh" 
) 

func getKeyFile() (key ssh.Signer, err error) { 
    //usr, _ := user.Current() 
    file := "Path to your key file(.pem)" 
    buf, err := ioutil.ReadFile(file) 
    if err != nil { 
     return 
    } 
    key, err = ssh.ParsePrivateKey(buf) 
    if err != nil { 
     return 
    } 
    return 
} 

func main() { 
    key, err := getKeyFile() 
    if err != nil { 
     panic(err) 
    } 

    // Define the Client Config as : 
    config := &ssh.ClientConfig{ 
     User: "root", 
     Auth: []ssh.AuthMethod{ 
      ssh.PublicKeys(key), 
     }, 
     HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error { 
      return nil 
     }, 
    } 
    client, err := ssh.Dial("tcp", "<remote ip>:22", config) 
    if err != nil { 
     panic("Failed to dial: " + err.Error()) 
    } 

    session, err := client.NewSession() 
    if err != nil { 
     panic("Failed to create session: " + err.Error()) 
    } 
    err = scp.CopyPath("local file path", "remote path", session) 
    if err != nil { 
     panic("Failed to Copy: " + err.Error()) 
    } 
    defer session.Close() 

는 희망이 도움이 아래 http://godoc.org/github.com/laher/scp-go/scp