42 lines
854 B
Go
42 lines
854 B
Go
package ssh
|
|
|
|
import (
|
|
"net"
|
|
|
|
"github.com/genofire/golang-lib/log"
|
|
"golang.org/x/crypto/ssh"
|
|
)
|
|
|
|
func (m *Manager) ExecuteEverywhere(cmd string) {
|
|
m.clientsMUX.Lock()
|
|
defer m.clientsMUX.Unlock()
|
|
for host, client := range m.clients {
|
|
m.execute(host, client, cmd)
|
|
}
|
|
}
|
|
|
|
func (m *Manager) ExecuteOn(addr net.TCPAddr, cmd string) error {
|
|
client, err := m.ConnectTo(addr)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return m.execute(addr.IP.String(), client, cmd)
|
|
}
|
|
|
|
func (m *Manager) execute(host string, client *ssh.Client, cmd string) error {
|
|
session, err := client.NewSession()
|
|
defer session.Close()
|
|
|
|
if err != nil {
|
|
log.Log.Warnf("can not create session on %s: %s", host, err)
|
|
delete(m.clients, host)
|
|
return err
|
|
}
|
|
err = session.Run(cmd)
|
|
if err != nil {
|
|
log.Log.Warnf("could not run %s on %s: %s", cmd, host, err)
|
|
return err
|
|
}
|
|
return nil
|
|
}
|