Connect to a server using SSH and a pem / key with golang

前端 未结 2 790
有刺的猬
有刺的猬 2021-01-02 07:07

I\'m trying to connect to an amazon AWS linux server with a key using the [ssh][1] package of Go programming language. However the package documentation is a bit cryptic/con

相关标签:
2条回答
  • 2021-01-02 07:10

    Here is an example to run ls remotely using your "plain private key file".

        pemBytes, err := ioutil.ReadFile("/location/to/YOUR.pem")
        if err != nil {
            log.Fatal(err)
        }
        signer, err := ssh.ParsePrivateKey(pemBytes)
        if err != nil {
            log.Fatalf("parse key failed:%v", err)
        }
        config := &ssh.ClientConfig{
            User: "ubuntu",
            Auth: []ssh.AuthMethod{ssh.PublicKeys(signer)},
        }
        conn, err := ssh.Dial("tcp", "yourhost.com:22", config)
        if err != nil {
            log.Fatalf("dial failed:%v", err)
        }
        defer conn.Close()
        session, err := conn.NewSession()
        if err != nil {
            log.Fatalf("session failed:%v", err)
        }
        defer session.Close()
        var stdoutBuf bytes.Buffer
        session.Stdout = &stdoutBuf
        err = session.Run("ls -l")
        if err != nil {
            log.Fatalf("Run failed:%v", err)
        }
        log.Printf(">%s", stdoutBuf)
    
    0 讨论(0)
  • 2021-01-02 07:20

    You need to use ssh.PublicKeys to turn a list of ssh.Signers into an ssh.AuthMethod. You can use ssh.ParsePrivateKey to get a Signer from the pem bytes, or if you need to use an rsa, dsa or ecdsa private key, you can give those to ssh.NewSignerFromKey.

    Here's an example fleshed out a bit with Agent support too (since using an agent is usually the next step after simply using a key file).

    sock, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK"))
    if err != nil {
        log.Fatal(err)
    }
    
    agent := agent.NewClient(sock)
    
    signers, err := agent.Signers()
    if err != nil {
        log.Fatal(err)
    }
    
    // or get the signer from your private key file directly
    // signer, err := ssh.ParsePrivateKey(pemBytes)
    // if err != nil {
    //     log.Fatal(err)
    // }
    
    auths := []ssh.AuthMethod{ssh.PublicKeys(signers...)}
    
    cfg := &ssh.ClientConfig{
        User: "username",
        Auth: auths,
    }
    cfg.SetDefaults()
    
    client, err := ssh.Dial("tcp", "aws-hostname:22", cfg)
    if err != nil {
        log.Fatal(err)
    }
    
    session, err = client.NewSession()
    if err != nil {
        log.Fatal(err)
    }
    
    log.Println("we have a session!")
    
    ...
    
    0 讨论(0)
提交回复
热议问题