https://github.com/brunotm/sshmgr
Go goroutine safe manager for SSH clients sharing between ssh/sftp sessions
https://github.com/brunotm/sshmgr
go golang goroutine-safe sftp ssh
Last synced: 7 months ago
JSON representation
Go goroutine safe manager for SSH clients sharing between ssh/sftp sessions
- Host: GitHub
- URL: https://github.com/brunotm/sshmgr
- Owner: brunotm
- License: apache-2.0
- Created: 2017-04-17T11:33:07.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2018-09-15T21:29:48.000Z (over 7 years ago)
- Last Synced: 2023-08-11T22:11:40.175Z (over 2 years ago)
- Topics: go, golang, goroutine-safe, sftp, ssh
- Language: Go
- Size: 228 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Go sshmgr
[](https://travis-ci.org/brunotm/sshmgr) [](https://goreportcard.com/report/github.com/brunotm/sshmgr)
====
### 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:
```go
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