Skip to content
/ sshmgr Public

Go goroutine safe manager for SSH clients sharing between ssh/sftp sessions

License

Notifications You must be signed in to change notification settings

brunotm/sshmgr

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

Go goroutine safe manager for SSH clients sharing between ssh/sftp sessions

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages