SSH Client example

2,043 views
Skip to first unread message

Sam Fourman Jr.

unread,
Jun 19, 2014, 2:45:38 AM6/19/14
to golan...@googlegroups.com
Hello list,

I am trying to build a ssh example and it isn't working... I am new to golang and I don't understand what is wrong.


Sam Fourman Jr.

unread,
Jun 19, 2014, 2:47:18 AM6/19/14
to golan...@googlegroups.com
When I try and build the following example:

I get this 
 
go build sam.go
# command-line-arguments
./sam.go:29: cannot assign *ssh.Signature to sig (type []byte) in multiple assignment
./sam.go:45: undefined: ssh.ClientAuth
./sam.go:56: undefined: ssh.ClientAuthKeyring
./sam.go:79: undefined: ssh.ClientAuth


--

Sam Fourman Jr.

Carlos Castillo

unread,
Jun 19, 2014, 2:29:01 PM6/19/14
to golan...@googlegroups.com
The code of the article is broken. 

The go.crypto/ssh package was changed on April 9th of this year and makes the original article (Russian: http://habrahabr.ru/post/215111/) obsolete as it was posted on March 9th.

Jianjun Mao

unread,
Jun 21, 2014, 10:00:05 AM6/21/14
to golan...@googlegroups.com
You can refer *_test.go files to see how to create config and dial.
For example, in https://code.google.com/p/go/source/browse/ssh/client_auth_test.go?repo=crypto you can find how to create authorisation methods. 

Jonathan Pittman

unread,
Jun 21, 2014, 2:20:12 PM6/21/14
to golan...@googlegroups.com
FWIW, if you just want to get this up and running, you can change all imports for "code.google.com/go.crypto/ssh" to "code.google.com/gosshold/ssh".  The old behavior should work fine.  The previous incarnation was retired there for those wanting simple updates.

I do, however, highly recommend you use the current go.crypto/ssh package and learn how to use it instead.

theka...@gmail.com

unread,
Apr 28, 2016, 12:06:06 PM4/28/16
to golang-nuts
Works with golang-1.6

package main

import (
    "bytes"
    "fmt"
    "log"
    "io/ioutil"
    "os"
    "time"
)

func executeCmd(cmd, hostname string, config *ssh.ClientConfig) string {
    conn, _ := ssh.Dial("tcp", hostname+":22", config)
    session, _ := conn.NewSession()
    defer session.Close()

    var stdoutBuf bytes.Buffer
    session.Stdout = &stdoutBuf
    session.Run(cmd)

    return hostname + ": " + stdoutBuf.String()
}

func main() {
    cmd := os.Args[1]
    hosts := os.Args[2:]
    results := make(chan string, 10)
    timeout := time.After(5 * time.Second)

    pkey, err := ioutil.ReadFile("/path/to/home/.ssh/id_rsa")
    if err != nil {
        log.Fatalf("unable to read private key: %v", err)
    }

    // Create the Signer for this private key.
    signer, err := ssh.ParsePrivateKey(pkey)
    if err != nil {
        log.Fatalf("unable to parse private key: %v", err)
    }

    config := &ssh.ClientConfig{
        User: os.Getenv("LOGNAME"),
        Auth: []ssh.AuthMethod{
            // Use the PublicKeys method for remote authentication.
            ssh.PublicKeys(signer),
        },
    }

    for _, hostname := range hosts {
        go func(hostname string) {
            results <- executeCmd(cmd, hostname, config)
        }(hostname)
    }

    for i := 0; i < len(hosts); i++ {
        select {
        case res := <-results:
            fmt.Print(res)
        case <-timeout:
            fmt.Println("Timed out!")
            return
Reply all
Reply to author
Forward
0 new messages