1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
6 Package ssh implements an SSH server.
8 SSH is a transport security protocol, an authentication protocol and a
9 family of application protocols. The most typical application level
10 protocol is a remote shell and this is specifically implemented. However,
11 the multiplexed nature of SSH is exposed to users that wish to support
14 An SSH server is represented by a Server, which manages a number of
15 ServerConnections and handles authentication.
18 s.PubKeyCallback = pubKeyAuth
19 s.PasswordCallback = passwordAuth
21 pemBytes, err := ioutil.ReadFile("id_rsa")
23 panic("Failed to load private key")
25 err = s.SetRSAPrivateKey(pemBytes)
27 panic("Failed to parse private key")
30 Once a Server has been set up, connections can be attached.
32 var sConn ServerConnection
34 err = sConn.Handshake(conn)
36 panic("failed to handshake")
39 An SSH connection multiplexes several channels, which must be accepted themselves:
43 channel, err := sConn.Accept()
45 panic("error from Accept")
51 Accept reads from the connection, demultiplexes packets to their corresponding
52 channels and returns when a new channel request is seen. Some goroutine must
53 always be calling Accept; otherwise no messages will be forwarded to the
56 Channels have a type, depending on the application level protocol intended. In
57 the case of a shell, the type is "session" and ServerShell may be used to
58 present a simple terminal interface.
60 if channel.ChannelType() != "session" {
61 c.Reject(UnknownChannelType, "unknown channel type")
66 shell := NewServerShell(channel, "> ")
70 line, err := shell.ReadLine()