shell

package module
v0.0.0-...-aca0665 Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2023 License: MIT Imports: 12 Imported by: 0

README

Go module that serves SSH sessions with an interactive shell. The interactive shell includes support for command history, which can be recalled using the up/down arrow keys.

Adding module

go get -u github.com/itzg/go-ssh-shell

Example

package main

import (
	shell "github.com/itzg/go-ssh-shell"
	"log"
)

type exampleHandler struct {
	s shell.Shell
}

func exampleHandlerFactory(s *shell.Shell) shell.Handler {
	return &exampleHandler{}
}

func (h *exampleHandler) HandleLine(line string) error {
	log.Printf("LINE from %s: %s", h.s.InstanceName(), line)
	return nil
}

func (h *exampleHandler) HandleEof() error {
	log.Printf("EOF from %s", h.s.InstanceName())
	return nil
}

func main() {
	sshServer := &shell.SshServer{
		Config: &shell.Config{
			Bind: ":2222",
			Users: map[string]shell.User{
				"user": {Password: "notsecure"},
			},
		},
		HandlerFactory: exampleHandlerFactory,
	}

	log.Fatal(sshServer.ListenAndServe())
}

Documentation

Index

Examples

Constants

View Source
const (
	DefaultConfigHistorySize = 100
	DefaultBind              = ":22"
)
View Source
const (
	ETX = 0x3 // control-C
	EOT = 0x4 // control-D
	BEL = 0x7
	BS  = 0x8
	LF  = 0xa
	CR  = 0xd
	ESC = 0x1b
	CSI = '['
	DEL = 0x7f
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Auth

type Auth struct {
	// contains filtered or unexported fields
}

func NewAuth

func NewAuth(config *Config) *Auth

func (*Auth) PasswordHandler

func (a *Auth) PasswordHandler(ctx ssh.Context, password string) bool

type Config

type Config struct {
	HistorySize int
	HostKeyFile string `mapstructure:"host-key-file"`
	Users       map[string]User
	Bind        string
}

type Handler

type Handler interface {
	// HandleLine is called with the next line that was consumed by the SSH shell. Typically this
	// is due the user typing a command string.
	// If an error is returned, then the error is reported back to the SSH client and the SSH
	// session is closed.
	HandleLine(line string) error

	// HandleEof is called when the user types Control-D
	// If an error is returned, then the error is reported back to the SSH client and the SSH
	// session is closed.
	HandleEof() error
}

type HandlerFactory

type HandlerFactory func(s *Shell) Handler

type HostKeyResolver

type HostKeyResolver struct {
	// contains filtered or unexported fields
}

func NewHostKeyResolver

func NewHostKeyResolver(config *Config) *HostKeyResolver

func (*HostKeyResolver) Resolve

func (r *HostKeyResolver) Resolve() string

func (*HostKeyResolver) ResolveOption

func (r *HostKeyResolver) ResolveOption() ssh.Option

type Shell

type Shell struct {
	// contains filtered or unexported fields
}

func NewShell

func NewShell(rw io.ReadWriter, instanceName string, config *Config) *Shell

func (*Shell) Add

func (s *Shell) Add(b byte) error

func (*Shell) AddString

func (s *Shell) AddString(val string) error

func (*Shell) Bell

func (s *Shell) Bell() error

func (*Shell) InstanceName

func (s *Shell) InstanceName() string

func (*Shell) OutputLine

func (s *Shell) OutputLine(line string) error

func (*Shell) Read

func (s *Shell) Read() (string, error)

Read blocks until the next line of input, such as a command, is available and returns that line. The error will be io.EOF if Control-D was pressed.

func (*Shell) Refresh

func (s *Shell) Refresh() error

func (*Shell) SetPrompt

func (s *Shell) SetPrompt(prompt string)

type SshServer

type SshServer struct {
	*Config
	HandlerFactory
}

SshServer manages acceptance of and authenticating SSH connections and delegating input to a Handler for each session instantiated by the given HandlerFactory. The zero value of this struct will use reasonable defaults, but won't be very useful since the default handler just outputs remote inputs to logs.

func (*SshServer) ListenAndServe

func (s *SshServer) ListenAndServe() error

ListenAndServe will block listening for new SSH connections and serving those with a new instance of Shell and a Handler each.

Example
package main

import (
	shell "github.com/itzg/go-ssh-shell"
	"log"
)

type exampleHandler struct {
	s shell.Shell
}

func exampleHandlerFactory(s *shell.Shell) shell.Handler {
	return &exampleHandler{}
}

func (h *exampleHandler) HandleLine(line string) error {
	log.Printf("LINE from %s: %s", h.s.InstanceName(), line)
	return nil
}

func (h *exampleHandler) HandleEof() error {
	log.Printf("EOF from %s", h.s.InstanceName())
	return nil
}

func main() {
	sshServer := &shell.SshServer{
		Config: &shell.Config{
			Bind: ":2222",
			Users: map[string]shell.User{
				"user": {Password: "notsecure"},
			},
		},
		HandlerFactory: exampleHandlerFactory,
	}

	log.Fatal(sshServer.ListenAndServe())
}
Output:

type User

type User struct {
	Password string
}

Jump to

Keyboard shortcuts

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