-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAddMyKeyPlease.go
63 lines (49 loc) · 1.16 KB
/
AddMyKeyPlease.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
54
55
56
57
58
59
60
61
62
package main
import (
"os"
"flag"
"log"
"encoding/base64"
"io/ioutil"
"strings"
)
func main() {
publicKey := flag.String("key", "none", "B64 Public key to be copied (required)")
username := flag.String("username", "root", "User to whom the key should be copied to")
flag.Parse()
if *publicKey == "none" {
log.Println("Usage: ")
flag.PrintDefaults()
os.Exit(1)
}
sshFile := "/root/.ssh/authorized_keys"
if *username != "root" {
sshFile = "/home/" + *username + "/.ssh/authorized_keys"
}
if _, err := os.Stat(sshFile); os.IsNotExist(err) {
log.Println("error: No such file or directory: ", sshFile)
log.Println("Make sure the authorized_keys file exist")
os.Exit(2)
}
f, err := os.OpenFile(sshFile, os.O_APPEND|os.O_WRONLY, 0600)
if err != nil {
panic(err)
}
defer f.Close()
b64Key, err := base64.StdEncoding.DecodeString(*publicKey)
if err != nil {
panic(err)
}
read, err := ioutil.ReadFile(sshFile)
if err != nil {
panic(err)
}
if !strings.Contains(string(read), string(b64Key)) {
if _, err = f.WriteString("\n" + string(b64Key)); err != nil {
panic(err)
}
} else {
log.Println("Key already exists")
os.Exit(0)
}
}