-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathmain.go
More file actions
79 lines (68 loc) · 1.59 KB
/
Copy pathmain.go
File metadata and controls
79 lines (68 loc) · 1.59 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"
"os"
"path/filepath"
"strings"
"github.com/kevinburke/ssh_config"
)
var (
hostName string
identityFile string
user string
key string
)
func main() {
sshConfigPath := filepath.Join(os.Getenv("HOME"), ".ssh", "config")
flag.StringVar(&key, "k", "", "host key to update")
flag.StringVar(&hostName, "h", "", "hostname")
flag.StringVar(&identityFile, "i", "", "identity file path")
flag.StringVar(&user, "u", "", "user")
flag.Parse()
f, err := os.Open(sshConfigPath)
if err != nil {
if os.IsNotExist(err) {
fmt.Fprintln(os.Stderr, "ssh config file not found, skipping update")
os.Exit(0)
} else {
panic(err)
}
}
defer f.Close()
cfg, err := ssh_config.Decode(f)
if err != nil {
panic(err)
}
matched := false
for _, host := range cfg.Hosts {
isWildcardOnly := len(host.Patterns) == 1 && host.Patterns[0].String() == "*"
if isWildcardOnly || !host.Matches(key) {
continue
}
matched = true
for _, node := range host.Nodes {
switch t := node.(type) {
case *ssh_config.KV:
if user != "" && strings.ToLower(t.Key) == "user" {
t.Value = user
}
if hostName != "" && strings.ToLower(t.Key) == "hostname" {
t.Value = hostName
}
if identityFile != "" && strings.ToLower(t.Key) == "identityfile" {
t.Value = identityFile
}
}
}
}
if !matched {
fmt.Fprintf(os.Stderr, "host %s not found in ssh config file, skipping update\n", key)
os.Exit(0)
} else {
bits, _ := cfg.MarshalText()
if err := os.WriteFile(sshConfigPath, bits, 0644); err != nil {
panic(err)
}
}
}