This repository was archived by the owner on Feb 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
79 lines (67 loc) · 1.44 KB
/
main.go
File metadata and controls
79 lines (67 loc) · 1.44 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"strings"
"github.com/wzshiming/ctc"
)
func init() {
flag.Parse()
}
func main() {
trusted := readKnownHosts(".db")
active := readKnownHosts("")
if flag.Arg(0) == "r" {
writeKnownHosts("", trusted)
fmt.Printf("%s%d hosts written%s\n", ctc.ForegroundGreen, len(trusted), ctc.Reset)
return
}
// check diff
temp := make([]string, 0)
for _, a := range active {
found := false
for _, t := range trusted {
if t == a {
found = true
break
}
}
if !found {
temp = append(temp, a)
}
}
if len(temp) > 0 {
fmt.Print(ctc.ForegroundYellow, "New hosts (", len(temp), ")", ctc.Reset, "\n")
for _, s := range temp {
fmt.Println(s)
}
} else {
fmt.Print(ctc.ForegroundGreen, "No new hosts\n", ctc.Reset)
}
}
func readKnownHosts(suffix string) []string {
res := make([]string, 0)
if f, err := os.Open(fmt.Sprint(os.Getenv("HOME"), "/.ssh/known_hosts", suffix)); err == nil {
defer f.Close()
b, err := ioutil.ReadAll(f)
if err == nil {
for _, s := range strings.Split(string(b), "\n") {
if strings.TrimSpace(s) != "" {
res = append(res, s)
}
}
}
}
return res
}
func writeKnownHosts(suffix string, lines []string) {
if f, err := os.OpenFile(fmt.Sprint(os.Getenv("HOME"), "/.ssh/known_hosts", suffix), os.O_WRONLY|os.O_APPEND, 0644); err == nil {
defer f.Close()
f.Truncate(0)
for _, line := range lines {
fmt.Fprintln(f, line)
}
}
}