sshtest

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 23, 2019 License: BSD-3-Clause Imports: 7 Imported by: 0

README

sshtest

sshtest is a set of utilities for testing SSH features in Go. It currently supports quick setup and start of SSH servers to test a client against. To minimize the need for error handling in tests, most functions panic on error.

Examples

Start a simple SSH server with host key
hostKey := sshtest.KeyFromFile("ssh-host-key", "")
server := sshtest.NewServer(hostKey)
defer server.Close()
Start an SSH server with host key and certificate
hostKey := sshtest.KeyFromFile("ssh-host-key", "ssh-host-key-cert.pub")
server := sshtest.NewServer(hostKey)
defer server.Close()
SSH server with custom server config and handler
hostKey := sshtest.KeyFromFile("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()

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

func KeyFromFile(keyfile, crtfile string) ssh.Signer

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

func NullHandler(ch ssh.Channel, in <-chan *ssh.Request)

NullHandler is used in SSH servers to discard all incoming requests and respond with success (code 0).

func SendStatus

func SendStatus(ch ssh.Channel, code uint32) error

SendStatus replies with an exits-status message on the provided channel.

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

func NewServer(hostKey ssh.Signer) *Server

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:

func (*Server) Close

func (s *Server) Close()

Close stops the Server's listener

func (*Server) Start

func (s *Server) Start()

Start the SSH server

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL