-
Notifications
You must be signed in to change notification settings - Fork 59
/
sshd.go
53 lines (43 loc) · 1.05 KB
/
sshd.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
42
43
44
45
46
47
48
49
50
51
52
53
package sshd
import (
"fmt"
"github.com/TNK-Studio/gortal/utils"
"github.com/fatih/color"
"github.com/gliderlabs/ssh"
"github.com/helloyi/go-sshclient"
)
// GetClientByPasswd GetClientByPasswd
func GetClientByPasswd(username, host string, port int, passwd string) (*sshclient.Client, error) {
client, err := sshclient.DialWithPasswd(
fmt.Sprintf("%s:%d", host, port),
username,
passwd,
)
if err != nil {
return nil, err
}
return client, nil
}
// Connect connect server
func Connect(host string, port int, username string, privKeyFile string, sess *ssh.Session) error {
client, err := sshclient.DialWithKey(
fmt.Sprintf("%s:%d", host, port),
username,
utils.FilePath(privKeyFile),
)
if err != nil {
return err
}
// default terminal
terminal := client.Terminal(nil)
terminal = terminal.SetStdio(*sess, *sess, *sess)
if terminal.Start(); err != nil {
return err
}
return nil
}
// ErrorInfo ErrorInfo
func ErrorInfo(err error, sess *ssh.Session) {
read := color.New(color.FgRed)
read.Fprint(*sess, fmt.Sprintf("%s\n", err))
}