-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
108 lines (100 loc) · 3.01 KB
/
main.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package main
import (
"flag"
"fmt"
"log"
"os"
"time"
"github.com/mbevc1/react/ssh"
"github.com/mbevc1/react/yaml"
)
var cmd = flag.String("cmd", "", "Command to run")
var bck = flag.Bool("b", false, "Backup MT device")
var bck_path = flag.String("bck-path", "backups", "Backup path")
var rbt = flag.Bool("r", false, "Reboot MT device")
var upg = flag.Bool("u", false, "Upgrade MT device")
var host = flag.String("h", "", "Host to connect")
var grp = flag.String("g", "", "Filter group")
var mts = flag.String("m", "mts.yaml", "Configuration file")
var hlp = flag.Bool("help", false, "Show command parameters")
var ver = flag.Bool("v", false, "Show version")
func init() {
// example with short version for long flag
flag.StringVar(cmd, "c", "", "Command to run")
}
func main() {
flag.Parse()
if *ver {
fmt.Printf("%s version %v\n", Name, version)
if commit != "" {
fmt.Printf("commit: %s\n", commit)
}
fmt.Printf("built at: %s\n", date)
fmt.Printf("built by: %s\n", builtBy)
os.Exit(0)
} else if *cmd == "" && !*bck && !*rbt && !*upg || *hlp {
fmt.Printf("%s version %v\n", Name, version)
if commit != "" {
fmt.Printf("commit: %s\n", commit)
}
fmt.Printf("built at: %s\n", date)
fmt.Printf("built by: %s\n", builtBy)
flag.PrintDefaults()
if *hlp {
os.Exit(0)
} else {
fmt.Println("Missing an action!")
os.Exit(1)
}
}
//Read Yaml
config := yaml.Parse(*mts)
for index, element := range config.Cfgs {
//Filter by group
if *grp != "" && *grp != element.Group {
continue
}
//Filter by host
if *host != "" && *host != element.Host {
continue
}
//Connect to host
ssh.Conn(&element.Host, &element.User, &element.Pass)
if *cmd != "" {
log.Printf("| Device %v: %v\nRunning...\n", index, element.Host)
ssh.Run(cmd)
log.Println("| done!")
}
if *bck {
log.Printf("| Device %v: %v\nBacking up...\n", index, element.Host)
dow := string([]rune(time.Now().Weekday().String())[0:3])
bck_dir := "backups"
dir := *bck_path + "/" + element.Group + "/" + element.Name
merr := os.MkdirAll(dir, os.ModePerm)
if merr != nil {
panic(merr)
}
//MT backup command
cmd := fmt.Sprintf("/export compact show-sensitive file=backup ; /tool fetch address=127.0.0.1 mode=ftp user=%s password=%s src-path=backup.rsc dst-path=%s/%s.rsc ; /file remove backup.rsc ; /system backup save name=%s/%s.backup", element.User, element.Pass, bck_dir, dow, bck_dir, dow)
ssh.Run(&cmd)
//Download locally
ssh.Download(bck_dir+"/"+dow+".rsc", dir+"/"+dow+".rsc")
ssh.Download(bck_dir+"/"+dow+".backup", dir+"/"+dow+".backup")
log.Println("| done!")
}
if *rbt {
cmd := "/system reboot;"
log.Printf("| Device %v: %v\nRebooting...\n", index, element.Host)
ssh.Run(&cmd)
log.Println("| done!")
}
if *upg {
cmd := "/system package update check; /system package update install; /system routerboard upgrade"
log.Printf("| Device %v: %v\nUpgrading...\n", index, element.Host)
ssh.Run(&cmd)
log.Println("| done!")
}
//Disconnect from host
ssh.Close()
}
}