public
Last active

an example of ssh client in golang

  • Download Gist
client.go
Go
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Modify by linuz.ly
package main
 
import (
"bytes"
"fmt"
"code.google.com/p/go.crypto/ssh"
"log"
)
var (
server = "127.0.0.1:22"
username = "username"
password = clientPassword("password")
)
type clientPassword string
func (p clientPassword) Password(user string) (string, error) {
return string(p), nil
}
type TerminalModes map[uint8]uint32
 
const (
VINTR = 1
VQUIT = 2
VERASE = 3
VKILL = 4
VEOF = 5
VEOL = 6
VEOL2 = 7
VSTART = 8
VSTOP = 9
VSUSP = 10
VDSUSP = 11
VREPRINT = 12
VWERASE = 13
VLNEXT = 14
VFLUSH = 15
VSWTCH = 16
VSTATUS = 17
VDISCARD = 18
IGNPAR = 30
PARMRK = 31
INPCK = 32
ISTRIP = 33
INLCR = 34
IGNCR = 35
ICRNL = 36
IUCLC = 37
IXON = 38
IXANY = 39
IXOFF = 40
IMAXBEL = 41
ISIG = 50
ICANON = 51
XCASE = 52
ECHO = 53
ECHOE = 54
ECHOK = 55
ECHONL = 56
NOFLSH = 57
TOSTOP = 58
IEXTEN = 59
ECHOCTL = 60
ECHOKE = 61
PENDIN = 62
OPOST = 70
OLCUC = 71
ONLCR = 72
OCRNL = 73
ONOCR = 74
ONLRET = 75
CS7 = 90
CS8 = 91
PARENB = 92
PARODD = 93
TTY_OP_ISPEED = 128
TTY_OP_OSPEED = 129
)
 
func main() {
// An SSH client is represented with a slete). Currently only
// the "password" authentication method is supported.
//
// To authenticate with the remote server you must pass at least one
// implementation of ClientAuth via the Auth field in ClientConfig.
 
config := &ssh.ClientConfig{
User: username,
Auth: []ssh.ClientAuth{
// ClientAuthPassword wraps a ClientPassword implementation
// in a type that implements ClientAuth.
ssh.ClientAuthPassword(password),
},
}
client, err := ssh.Dial("tcp", "127.0.0.1:22", config)
if err != nil {
panic("Failed to dial: " + err.Error())
}
 
// Each ClientConn can support multiple interactive sessions,
// represented by a Session.
defer client.Close()
// Create a session
session, err := client.NewSession()
if err != nil {
log.Fatalf("unable to create session: %s", err)
}
defer session.Close()
// Set up terminal modes
modes := ssh.TerminalModes{
ECHO: 0, // disable echoing
TTY_OP_ISPEED: 14400, // input speed = 14.4kbaud
TTY_OP_OSPEED: 14400, // output speed = 14.4kbaud
}
// Request pseudo terminal
if err := session.RequestPty("xterm", 80, 40, modes); err != nil {
log.Fatalf("request for pseudo terminal failed: %s", err)
}
 
var b bytes.Buffer
session.Stdout = &b
if err := session.Run("ls"); err != nil {
panic("Failed to run: " + err.Error())
}
fmt.Println(b.String())
 
}

Hi there,

I wonder if you can help me out. I was trying your client, and it connects just fine to my linux box, but when I try a different system, I get an error stating that "ssh: no common algorithms"

When I use putting to connect to the system with the issues, here is the log. I don't see anything unusual that shouldn't be supported by crypto/ssh

Putty Negotiation

2013-09-16 10:41:35 Looking up host "10.0.0.1"
2013-09-16 10:41:35 Connecting to 10.0.0.1. port 22
2013-09-16 10:41:35 Server version: SSH-2.0-ConfD
2013-09-16 10:41:35 We claim version: SSH-2.0-PuTTY_Release_0.60
2013-09-16 10:41:35 Using SSH protocol version 2
2013-09-16 10:41:35 Using Diffie-Hellman with standard group "group1"
2013-09-16 10:41:35 Doing Diffie-Hellman key exchange with hash SHA-1
2013-09-16 10:41:35 Host key fingerprint is:
2013-09-16 10:41:35 ssh-dss 1024
2013-09-16 10:41:35 Initialised triple-DES CBC client->server encryption
2013-09-16 10:41:35 Initialised HMAC-SHA1 client->server MAC algorithm
2013-09-16 10:41:35 Initialised triple-DES CBC server->client encryption
2013-09-16 10:41:35 Initialised HMAC-SHA1 server->client MAC algorithm
2013-09-16 10:41:38 Sent password
2013-09-16 10:41:38 Access granted
2013-09-16 10:41:38 Opened channel for session
2013-09-16 10:41:38 Allocated pty (ospeed 38400bps, ispeed 38400bps)
2013-09-16 10:41:38 Started a shell/command

Had the same problem and I was informed that my ssh server and this golang client used different cihpers, which was the reason for the "no common algorithms" I just used a different ssh server and it works fine now.

My problem now is how to execute commands like creating, moving, and copying of files between local and remote?

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.