freifunkmanager/ssh/execute.go

42 lines
854 B
Go
Raw Normal View History

2017-05-06 14:37:24 +02:00
package ssh
import (
"net"
2017-05-29 22:55:38 +02:00
"github.com/genofire/golang-lib/log"
2017-05-06 14:37:24 +02:00
"golang.org/x/crypto/ssh"
)
func (m *Manager) ExecuteEverywhere(cmd string) {
2017-07-08 19:00:36 +02:00
m.clientsMUX.Lock()
defer m.clientsMUX.Unlock()
2017-05-06 14:37:24 +02:00
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)
2017-05-06 14:37:24 +02:00
}
func (m *Manager) execute(host string, client *ssh.Client, cmd string) error {
2017-05-06 14:37:24 +02:00
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
2017-05-06 14:37:24 +02:00
}
err = session.Run(cmd)
if err != nil {
log.Log.Warnf("could not run %s on %s: %s", cmd, host, err)
return err
2017-05-06 14:37:24 +02:00
}
return nil
2017-05-06 14:37:24 +02:00
}