Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/desops/sshpool
Connection pool for x/crypto/ssh connections
https://github.com/desops/sshpool
go golang
Last synced: 30 days ago
JSON representation
Connection pool for x/crypto/ssh connections
- Host: GitHub
- URL: https://github.com/desops/sshpool
- Owner: desops
- License: mit
- Created: 2020-12-18T01:44:18.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2024-06-28T09:06:53.000Z (6 months ago)
- Last Synced: 2024-08-03T23:18:22.600Z (4 months ago)
- Topics: go, golang
- Language: Go
- Homepage:
- Size: 28.3 KB
- Stars: 7
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-golang-repositories - sshpool
README
# sshpool
Connection pool for x/crypto/ssh connectionsAPI docs: https://pkg.go.dev/github.com/desops/sshpool
This is a connection pooler for golang.org/x/crypto/ssh. It's good for making hundreds (or thousands) of SSH connections to do a lot of things on a lot of hosts.
The pool itself has no configuration apart from a ClientConfig:
```go
package mainimport (
"fmt"
"io/ioutil"
"os""github.com/desops/sshpool"
"golang.org/x/crypto/ssh"
)func main() {
if err := run(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}func run() error {
buf, err := ioutil.ReadFile("./keys/my_private_ssh_key")
if err != nil {
return err
}key, err := ssh.ParsePrivateKey(buf)
if err != nil {
return err
}config := &ssh.ClientConfig{
User: "myuser",
Auth: []ssh.AuthMethod{
ssh.PublicKeys(key),
},
}pool := sshpool.New(config, nil)
for i := 0; i < 100; i++ {
go func() {
err := func() error {
session, err := pool.Get("myhost")
if err != nil {
return err
}
defer session.Put() // important: this returns it to the poolsession.Stdout = os.Stdout
session.Stderr = os.Stderrif err := session.Run("sleep 10"); err != nil {
ee, ok := err.(*ssh.ExitError)
if ok {
return fmt.Errorf("remote command exit status %d", ee.ExitStatus())
}
return err
}
return nil
}
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
}()
}return nil
}
```