Documentation
¶
Index ¶
- func StartInteractiveSession(client *ssh.Client) error
- type Client
- func (c *Client) Close() error
- func (c *Client) Connect(host string) error
- func (c *Client) Dial(network, addr string) (net.Conn, error)
- func (c *Client) Execute(cmd string) (string, error)
- func (c *Client) ForwardLocal(localAddr, remoteAddr string) error
- func (c *Client) NewSession() (*ssh.Session, error)
- func (c *Client) RawClient() *ssh.Client
- func (c *Client) StartInteractive() error
- type ClientOptionFn
- type WinChangeHandler
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func StartInteractiveSession ¶
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 ¶
Close closes the SSH connection and releases resources. It is safe to call Close multiple times.
func (*Client) Connect ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
RawClient returns the underlying ssh.Client for advanced usage. Returns nil if not connected.
func (*Client) StartInteractive ¶
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