sshmgr

package module
v0.0.0-...-09ed004 Latest Latest
Warning

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

Go to latest
Published: Sep 15, 2018 License: Apache-2.0 Imports: 12 Imported by: 1

README

Go sshmgr

Build Status Go Report Card

A goroutine safe manager for SSH and SFTP client sharing.

It makes possible to share and reutilize existing clients for the same host made with the same user,port and credentials between multiple goroutines.

Clients are reference counted, and automatically closed/removed from the manager when they have no references and the client TTL is exceeded.


Usage:

package main

import (
	"fmt"
	"io/ioutil"
	"time"

	"github.com/brunotm/sshmgr"
)

func main() {

	// Creates a manager with a client ttl of 10 seconds and
	// a GC interval of 5 seconds
	manager := sshmgr.New(time.Second*10, time.Second*5)
	defer manager.Close()

	key, err := ioutil.ReadFile("/path/to/key")
	if err != nil {
		panic(err)
	}

	config := sshmgr.ClientConfig{}
	config.NetAddr = "hosta"
	config.Port = "22"
	config.User = "root"
	config.Password = ""
	config.Key = key
	config.IgnoreHostKey = true
	config.ConnDeadline = time.Minute
	config.DialTimeout = time.Second * 5

	client, err := manager.SSHClient(config)
	if err != nil {
		panic(err)
	}
	// Must close the client when done.
	defer client.Close()

	data, err := client.CombinedOutput("uptime", nil)
	if err != nil {
		panic(err)
	}

	fmt.Printf("%s: %s", config.NetAddr, string(data))
}

Written by Bruno Moura brunotm@gmail.com

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

Client is a shared managed ssh client

func (*Client) Close

func (c *Client) Close() (err error)

Close notifies the manager that this client can be removed if there is no more references to it

func (*Client) CombinedOutput

func (c *Client) CombinedOutput(cmd string, envs map[string]string) (data []byte, err error)

CombinedOutput runs cmd on the remote host and returns its combined standard output and standard error.

func (*Client) CombinedReader

func (c *Client) CombinedReader(cmd string, envs map[string]string) (reader io.ReadCloser, err error)

CombinedReader is like CombinedOutput but returns a io.Reader combining both stderr and stdout.

type ClientConfig

type ClientConfig struct {
	// NetAddr specifies the host ip or name
	NetAddr string

	// Port specifies the host port to connect to.
	// Defaults to 22 if empty
	Port string

	// User to authenticate as
	User string

	// Password to authenticate with
	Password string

	// Key to authenticate with
	Key []byte

	// IgnoreHostKey specifies whether to use InsecureIgnoreHostKey as the
	// HostKeyCallback to disable host key verification
	IgnoreHostKey bool

	// Deadline to be used in the underlying net.Conn.
	// Specified as a time.Duration so its set as the sum of the current time
	// and the ConnDeadline when the connection is established or to upgrade the
	// deadline when reusing a client
	ConnDeadline time.Duration

	// DialTimeout
	DialTimeout time.Duration
}

ClientConfig parameters for getting ssh or sftp clients from the manager

type Manager

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

Manager for shared ssh and sftp clients

func New

func New(clientTTL, gcInterval time.Duration) (manager *Manager)

New creates a new Manager. clientTTL specifies the maximum amount of time after which it was last accessed that client will be kept alive in the manager without open references. The client last access time is updated when the client is released gcInterval specifies the interval the manager will try to remove unused clients

func (*Manager) Close

func (m *Manager) Close()

Close all running clients and shutdown the manager

func (*Manager) SFTPClient

func (m *Manager) SFTPClient(config ClientConfig) (session *SFTPClient, err error)

SFTPClient creates a session from a active managed client or create a new one on demand. Clients must be closed after usage so they can be removed when they have no references

func (*Manager) SSHClient

func (m *Manager) SSHClient(config ClientConfig) (client *Client, err error)

SSHClient returns an active managed client or create a new one on demand. Clients must be closed after usage so they can be removed when there are no references

type SFTPClient

type SFTPClient struct {
	*sftp.Client
	// contains filtered or unexported fields
}

SFTPClient type

func (*SFTPClient) Close

func (s *SFTPClient) Close() (err error)

Close the session and notify the manager

func (*SFTPClient) Lock

func (s *SFTPClient) Lock()

Lock overrides the original client channel lock (does nothing)

func (*SFTPClient) Unlock

func (s *SFTPClient) Unlock()

Unlock overrides the original client channel lock (does nothing)

Directories

Path Synopsis
Package locker provides a mechanism for creating finer-grained locking to help free up more global locks to handle other tasks.
Package locker provides a mechanism for creating finer-grained locking to help free up more global locks to handle other tasks.

Jump to

Keyboard shortcuts

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