freifunkmanager/ssh/execute.go

31 lines
545 B
Go
Raw Normal View History

2017-05-06 14:37:24 +02:00
package ssh
import (
"net"
2019-06-10 02:57:36 +02:00
"github.com/bdlm/log"
2018-06-30 01:45:51 +02:00
2017-05-06 14:37:24 +02:00
"golang.org/x/crypto/ssh"
)
func (m *Manager) ExecuteOn(addr net.TCPAddr, cmd string) error {
client, err := m.ConnectTo(addr)
if err != nil {
return err
}
2018-06-30 01:45:51 +02:00
defer client.Close()
return Execute(addr.IP.String(), client, cmd)
2017-05-06 14:37:24 +02:00
}
func Execute(host string, client *ssh.Client, cmd string) error {
2017-05-06 14:37:24 +02:00
session, err := client.NewSession()
defer session.Close()
err = session.Run(cmd)
if err != nil {
2018-06-30 01:45:51 +02:00
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
}