-
Notifications
You must be signed in to change notification settings - Fork 0
/
ssh.go
41 lines (34 loc) · 974 Bytes
/
ssh.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package helpers
import (
ssh "github.com/helloyi/go-sshclient"
"github.com/minskylab/meta-core/config"
"github.com/minskylab/meta-core/services/structures"
log "github.com/sirupsen/logrus"
)
func ConnectRemote(conf *config.Config, instance *structures.EC2Instance, keyPair *structures.KeyPair, username string) (*ssh.Client, error) {
host := instance.PublicIp + ":22"
if conf.Debug {
log.Debugf("ssh -i %s %s@%s", *keyPair.FilePath, username, host)
}
client, err := ssh.DialWithKey(host, username, *keyPair.FilePath)
if err != nil {
return nil, err
}
return client, nil
}
func ExecuteRemote(conf *config.Config, client *ssh.Client, cmd string) string {
log.Debugln(cmd)
out, err := client.Cmd(cmd).SmartOutput()
if err != nil {
// the 'out' is stderr output
if conf.Debug {
log.Debugln("error: ", out)
}
return string(out)
}
if conf.Debug {
log.Debugln("output: ", string(out))
}
// the 'out' is stdout output
return string(out)
}