-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
135 lines (122 loc) · 3.94 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package main
import (
"flag"
"fmt"
"log"
"os"
"strings"
"time"
"github.com/Rldeckard/sshRunCMD/closeHandler"
"github.com/Rldeckard/sshRunCMD/userPrompt"
"github.com/cheggaaa/pb/v3"
"github.com/spf13/viper"
"github.com/zenthangplus/goccm"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/widget"
)
type CRED struct {
username string
password string
fallbackUser string
fallbackPass string
privatekey string
core string
pingTimeout int
pingCount int
}
type Progress struct {
offlineDevices []string
unauthedDevices []string
connectedDevices []string
failedCommandsDevices []string
failedCommands []string
step float64
}
var originalOutput = flag.Bool("s", false, "Shows raw output from switches.")
var testRun = flag.Bool("t", false, "Run preloaded test case for development. Defined in helper file.")
var verboseOutput = flag.Bool("v", false, "Output all successfully connected devices.")
var verifyCreds = flag.Bool("c", false, "Doesn't verify your credentials against a known device. Be careful to not lock out your account.")
var promptCreds = flag.Bool("p", false, "Bypasses all stored credentials and prompts for new credentials.")
var fileOutput = flag.Bool("f", false, "Sends output from switches to file. Good for show runs or cdp neighbor.")
// -g=false to call flag
var showGUI = flag.Bool("g", true, "Disables GUI. Ex. -g=false")
var helpCalled = flag.Bool("h", false, "Shows Usage Menu.")
var progress Progress
var outputCMD = widget.NewLabel("")
var progBar *widget.ProgressBar
var myWindow fyne.Window
var useCreds = false
var legacySSH = false
// encryption key used to decrypt helper.yml
// create 'helper.key' file to store appCode. Copy below code format for yml
// helper:
//
// key: 'fasdfasdfasdfasdf'
var appCode string
// to build new fyne application: 'fyne package -target windows -icon .\sshRunCMD.png'
func main() {
log.SetFlags(log.LstdFlags | log.Lshortfile)
// This sets certain flags for the "log" package, so when a log.Println
// or other sub-function is run, makes a traceback. Similar to log.Panic, but does it for every log function.
// The log package contains many of the same functions as fmt.
flag.Parse()
closeHandler.Listener()
os.Remove("output.txt")
var cred CRED
var deviceList []string
var userScript []string
if *helpCalled {
flag.PrintDefaults()
os.Exit(0)
}
GetCredentialsFromFiles(&cred)
if cred.username == "" || *promptCreds {
if !*promptCreds {
log.Println("Unable to read credentials from helper file.")
}
if !*showGUI {
cred.username = prompt.Credentials("Username:")
cred.password = prompt.Credentials("Password:")
}
}
if !*showGUI {
if !*testRun {
deviceList = prompt.List("Enter Device List, Press Enter when completed.")
userScript = prompt.List("Enter commands to run, Press Enter when completed.")
} else {
deviceList = viper.GetStringSlice("tester.devices")
userScript = strings.Split(viper.GetString("tester.commands"), ",") //only works for one command, but needs to be a slice to be processed. Possible convert to csv import if needed.
}
fmt.Println("Received input, processing...")
waitGroup := goccm.New(40)
checkList := strings.Split(deviceList[0], " ")
if len(checkList) > 1 {
deviceList = checkList
}
bar := pb.StartNew(len(deviceList)).SetTemplate(pb.Simple).SetRefreshRate(25 * time.Millisecond) //Default refresh rate is 200 Milliseconds.
for _, deviceIP := range deviceList {
waitGroup.Wait()
go func(host string) {
defer bar.Increment()
defer waitGroup.Done()
err := cred.SSHConnect(userScript, host)
if err != nil {
log.Fatalf("issue with ssh: %v", err)
}
}(deviceIP)
}
//blocks until ALL go routines are done.
waitGroup.WaitAllDone()
for i := uint8(0); i <= 50; i++ {
if bar.Current() == int64(len(deviceList)) {
bar.Finish()
break
}
time.Sleep(5 * time.Millisecond)
}
showResults()
prompt.Pause()
} else {
cred.guiApp()
}
}