-
Notifications
You must be signed in to change notification settings - Fork 59
/
copyid.go
39 lines (31 loc) · 879 Bytes
/
copyid.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
package sshd
import (
"fmt"
"io/ioutil"
"os"
"strings"
"github.com/TNK-Studio/gortal/utils"
"github.com/TNK-Studio/gortal/utils/logger"
)
// CopyID CopyID
func CopyID(username, host string, port int, passwd, pubKeyFile string) ([]byte, error) {
client, err := GetClientByPasswd(username, host, port, passwd)
if err != nil {
return []byte(""), err
}
file, err := os.Open(utils.FilePath(pubKeyFile))
if err != nil {
return []byte(""), err
}
defer file.Close()
b, err := ioutil.ReadAll(file)
pubKey := fmt.Sprintf("%s %s@%s", string(b), username, host)
copyIDCmd := fmt.Sprintf("echo \"%s\" >> ~/.ssh/authorized_keys", pubKey)
copyIDCmd = strings.ReplaceAll(copyIDCmd, "\n", "")
logger.Logger.Debugf("CopyID run command:\n%s", copyIDCmd)
output, err := client.Cmd(copyIDCmd).Output()
if err != nil {
return []byte(""), err
}
return output, nil
}