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"
|
|
|
|
)
|
|
|
|
|
2017-05-30 11:17:16 +02:00
|
|
|
func (m *Manager) ExecuteOn(addr net.TCPAddr, cmd string) error {
|
|
|
|
client, err := m.ConnectTo(addr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2017-05-06 19:44:37 +02:00
|
|
|
}
|
2018-06-30 01:45:51 +02:00
|
|
|
defer client.Close()
|
2018-06-30 16:20:54 +02:00
|
|
|
return Execute(addr.IP.String(), client, cmd)
|
2017-05-06 14:37:24 +02:00
|
|
|
}
|
|
|
|
|
2018-06-30 16:20:54 +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)
|
2017-05-30 11:17:16 +02:00
|
|
|
return err
|
2017-05-06 14:37:24 +02:00
|
|
|
}
|
2017-05-30 11:17:16 +02:00
|
|
|
return nil
|
2017-05-06 14:37:24 +02:00
|
|
|
}
|