60 lines
1.1 KiB
Go
60 lines
1.1 KiB
Go
package ssh
|
|
|
|
import (
|
|
"sync"
|
|
|
|
"golang.org/x/crypto/ssh"
|
|
)
|
|
|
|
type List struct {
|
|
Command string `json:"cmd"`
|
|
Clients map[string]*ListResult `json:"clients"`
|
|
sshManager *Manager
|
|
sync.Mutex
|
|
}
|
|
type ListResult struct {
|
|
ssh *ssh.Client
|
|
Running bool `json:"running"`
|
|
WithError bool `json:"with_error"`
|
|
Result string `json:"result"`
|
|
}
|
|
|
|
func (m *Manager) CreateList(cmd string) *List {
|
|
list := &List{
|
|
Command: cmd,
|
|
sshManager: m,
|
|
Clients: make(map[string]*ListResult),
|
|
}
|
|
m.clientsMUX.Lock()
|
|
defer m.clientsMUX.Unlock()
|
|
for host, client := range m.clients {
|
|
list.Clients[host] = &ListResult{Running: true, ssh: client}
|
|
}
|
|
return list
|
|
}
|
|
|
|
func (l List) Run() {
|
|
wg := new(sync.WaitGroup)
|
|
for host, client := range l.Clients {
|
|
wg.Add(1)
|
|
go l.runlistelement(host, client, wg)
|
|
|
|
}
|
|
wg.Wait()
|
|
}
|
|
|
|
func (l List) runlistelement(host string, client *ListResult, wg *sync.WaitGroup) {
|
|
defer wg.Done()
|
|
result, err := l.sshManager.run(host, client.ssh, l.Command)
|
|
|
|
l.Lock()
|
|
defer l.Unlock()
|
|
|
|
client.Running = false
|
|
if err != nil {
|
|
client.WithError = true
|
|
return
|
|
}
|
|
client.Result = SSHResultToString(result)
|
|
}
|