sshc

package
v0.0.0-...-10fbe9a Latest Latest
Warning

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

Go to latest
Published: May 6, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func StartInteractiveSession

func StartInteractiveSession(client *ssh.Client) error

StartInteractiveSession starts an interactive terminal session using the provided SSH client. It performs the following:

  • Creates a new SSH session
  • Sets the local terminal to raw mode
  • Requests a PTY (pseudo-terminal) with the current terminal size
  • Redirects stdin/stdout/stderr to the session
  • Handles terminal resize events (on Unix systems)
  • Waits for the session to end

The function blocks until the session ends (user types 'exit', presses Ctrl+D, or the connection is closed). After the session ends, it restores the terminal to its original state.

Note: On Windows, terminal resize events are not supported.

Example:

client, err := ssh.Dial("tcp", "host:22", config)
if err != nil {
    log.Fatal(err)
}
defer client.Close()

if err := sshc.StartInteractiveSession(client); err != nil {
    log.Fatal(err)
}

Types

type Client

type Client struct {
	// Username is the SSH login username
	Username string
	// Password is the SSH login password (used for password auth or as passphrase for private key)
	Password string
	// PrivateKey is the path to the private key file for key-based authentication
	PrivateKey string
	// Port is the SSH server port (default: 22)
	Port int
	// Timeout is the connection timeout duration (default: 30s)
	Timeout time.Duration
	// Keepalive is the interval for sending keepalive packets (default: 30s, 0 to disable)
	Keepalive time.Duration
	// StrictHost enables strict host key checking when set to true
	StrictHost bool
	// contains filtered or unexported fields
}

Client represents an SSH client connection with configuration options. It provides methods for connecting to remote servers, executing commands, starting interactive sessions, and port forwarding.

func NewClient

func NewClient(username, password string, opts ...ClientOptionFn) *Client

NewClient creates a new SSH client with the given username and password. Default values: Port=22, Timeout=30s, Keepalive=30s, StrictHost=false.

Example:

client := sshc.NewClient("root", "password")
client.Port = 2222
client.Timeout = 60 * time.Second
if err := client.Connect("192.168.1.100"); err != nil {
    log.Fatal(err)
}
defer client.Close()

func (*Client) Close

func (c *Client) Close() error

Close closes the SSH connection and releases resources. It is safe to call Close multiple times.

func (*Client) Connect

func (c *Client) Connect(host string) error

Connect establishes a TCP connection to the SSH server at the given host. It authenticates using the configured credentials (password and/or private key). If Keepalive is set, it starts a background goroutine to send keepalive packets.

The host parameter should be the hostname or IP address without port (use Client.Port for port).

func (*Client) Dial

func (c *Client) Dial(network, addr string) (net.Conn, error)

Dial establishes a connection to the remote address through the SSH tunnel. It uses the SSH client's Dial method to create the connection.

The network parameter should be "tcp" or "udp". The addr parameter should be in the format "host:port".

Returns an error if the client is not connected.

func (*Client) Execute

func (c *Client) Execute(cmd string) (string, error)

Execute runs a command on the remote server and returns the combined output. It creates a new session, runs the command, and closes the session automatically.

Example:

output, err := client.Execute("ls -la /home")
if err != nil {
    log.Fatal(err)
}
fmt.Println(output)

func (*Client) ForwardLocal

func (c *Client) ForwardLocal(localAddr, remoteAddr string) error

ForwardLocal creates a local port forward from localAddr to remoteAddr. It listens on localAddr and forwards connections to remoteAddr through the SSH tunnel.

This is a blocking function that runs indefinitely until an error occurs. For typical use, run it in a goroutine.

Example:

// Forward local port 8080 to remote port 80
go client.ForwardLocal("localhost:8080", "localhost:80")

func (*Client) NewSession

func (c *Client) NewSession() (*ssh.Session, error)

NewSession creates a new SSH session on the established connection. The caller is responsible for closing the session when done.

Returns an error if the client is not connected.

func (*Client) RawClient

func (c *Client) RawClient() *ssh.Client

RawClient returns the underlying ssh.Client for advanced usage. Returns nil if not connected.

func (*Client) StartInteractive

func (c *Client) StartInteractive() error

StartInteractive starts an interactive terminal session with the remote server. It sets up PTY (pseudo-terminal) with the current terminal size and handles window resize events on Unix systems.

The function blocks until the session ends (user exits or connection closes).

Returns an error if the client is not connected.

type ClientOptionFn

type ClientOptionFn func(*Client)

type WinChangeHandler

type WinChangeHandler interface {
	// Stop stops listening for window change events and releases resources.
	Stop()
}

WinChangeHandler is an interface for handling terminal window resize events. Implementations are platform-specific:

  • On Unix systems: handles SIGWINCH signals to resize the PTY
  • On Windows: no-op since SIGWINCH is not available

Jump to

Keyboard shortcuts

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