Skip to content

Latest commit

 

History

History
121 lines (89 loc) · 2.76 KB

README.md

File metadata and controls

121 lines (89 loc) · 2.76 KB

sshutil

A single-dependency utility package that provides a net/http style SSH server.

sshutil is part of the Smallstep crypto suite (step, step-ca, etc.).

Why sshutil?

The sshutil package depends solely on the Go x/crypto module. The x/crypto/ssh package provides convenient support for the ssh wire protocol, the ssh authentication protocol, and the ssh connection protocol. SSH, and thus the x/crypto implementation, is natually scoped to a single connection—whereas servers generally need to accept many connections. A small, but tedious, amount of work is required to implement a full connection-tracking server for use in applications. sshutil fills in the gap.

Get

$ go get go.step.sm/sshutil

Examples

Example can be found in the examples directory. Run with:

$ go run go.step.sm/example/<name>
$ go run ./example/<name>

Hello SSH

hello

package main

import "go.step.sm/sshutil"

func() hello(stream sshutil.Session) {
	stream.Terminal.Write([]byte("Hello SSH\n")
}

func main() {
	server := &sshutil.Server{Addr: ":2022"}
	server.Channel("session", sshutil.NewSessionHandler(hello))
	server.ListenAndServe()
}

Output:

$ go run ./example/hello
$ ssh localhost -p 2022
Hello SSH
Server closed remote connection to localhost.

Host Key

Easily configure a persistent host key using sshutil.LoadHostKeyFromFile. The default session handler is an echo terminal server.

hostkey

package main

import (
	"log"

	"go.step.sm/sshutil"
)

func main() {
	server := &sshutil.Server{
		Addr: ":2022",
		Config: sshutil.DefaultServerConfig(),
	}

	key, err := sshutil.LoadHostKeyFromFile("example/server.key", "")
	if err != nil {
		log.Fatalf("error loading key: %v", err)
	}
	server.Config.AddHostKey(key)

	err = server.ListenAndServe()
	log.Print(err)
}

Output:

$ go run ./example/hostkey
$ ssh localhost -p 2022
> echo
echo
> ^D
Client closed connection to localhost.

Test

$ go test