Documentation ¶
Overview ¶
Package sshtest provides utilities for SSH testing.
Example ¶
package main import ( "fmt" "github.com/folbricht/sshtest" ) func main() { hostKey := sshtest.KeyFromFile("testdata/ssh-host-key", "") server := sshtest.NewServer(hostKey) defer server.Close() fmt.Println("SSH server started on:", server.Endpoint) }
Output:
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func KeyFromFile ¶
KeyFromFile reads a private key and optionally a certificate file in OpenSSH format and returns an ssh.Signer. If crtfile is not the empty string, the Signer will be using the SSH certificate from it.
func NullHandler ¶
NullHandler is used in SSH servers to discard all incoming requests and respond with success (code 0).
Types ¶
type Server ¶
type Server struct { Endpoint string Listener net.Listener Config *ssh.ServerConfig // Handler for incoming sessions, NullHandler if nil Handler func(ssh.Channel, <-chan *ssh.Request) // contains filtered or unexported fields }
A Server is an SSH server listening on a random port on the loopback interface. This server can be used in tests for SSH clients.
func NewServer ¶
NewServer starts and return a new SSH server. The caller is responsible for calling Close() when done.
func NewUnstartedServer ¶
func NewUnstartedServer() *Server
NewUnstartedServer returns a new server with the default config but doesn't start it allowing the caller to change the config or add keys before starting the server.
Example ¶
package main import ( "fmt" "github.com/folbricht/sshtest" "golang.org/x/crypto/ssh" ) func main() { hostKey := sshtest.KeyFromFile("testdata/ssh-host-key", "") server := sshtest.NewUnstartedServer() server.Config = &ssh.ServerConfig{NoClientAuth: true} server.Config.AddHostKey(hostKey) server.Handler = func(ch ssh.Channel, in <-chan *ssh.Request) { defer ch.Close() // Read a request from the client req, ok := <-in if !ok { return } fmt.Printf("Received '%s' request from client", req.Type) // Reply with a string req.Reply(true, []byte("Hello client")) // Let the client know the command completed successfuly (status=0) sshtest.SendStatus(ch, 0) } server.Start() defer server.Close() }
Output: