-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
135 lines (101 loc) · 2.81 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 (
"fmt"
"os"
"path/filepath"
"runtime"
"sort"
"strings"
"time"
"github.com/briandowns/spinner"
)
var spin = spinner.New(spinner.CharSets[2], 100*time.Millisecond)
func standby() {
fmt.Println("Press Enter Key to exit.")
fmt.Scanln()
}
func main() {
spin.Prefix = "Performing nmap check "
spin.Start()
nmapV, err := CheckNmap()
spin.Stop()
if err != nil {
fmt.Println("Nmap is required. Please install by visiting the link below.")
fmt.Println()
if runtime.GOOS == "windows" {
fmt.Println("Official Website: https://nmap.org/download#windows")
fmt.Println("Chocolatey: https://community.chocolatey.org/packages/nmap#install")
}
if runtime.GOOS == "darwin" {
fmt.Println("Official Website: https://nmap.org/download.html#macosx")
fmt.Println("Homebrew: https://formulae.brew.sh/formula/nmap")
}
fmt.Println()
standby()
return
}
fmt.Println(nmapV)
nmapFile := "nmap.xml"
nmapFile = filepath.Join(os.TempDir(), nmapFile)
currentIp, err := GetOutboundIP()
if err != nil {
fmt.Println(err)
standby()
return
}
currentIpStr := currentIp.String()
ipRange := currentIpStr[0:strings.LastIndex(currentIpStr, ".")] + ".0-255"
spin.Prefix = "Scanning network " + ipRange + " "
spin.Start()
b, err := ExecuteCmd("nmap", "--unprivileged", "--script-timeout", "2", "-d", "-oX", nmapFile, "-p", "80", ipRange)
spin.Stop()
if err != nil {
fmt.Println(b, err)
standby()
return
}
if _, err := os.Stat(nmapFile); err != nil {
fmt.Println(err)
standby()
return
}
spin.Prefix = "Looking for tasmota devices... "
spin.Start()
nmapOutput, err := GetNmapOutput(nmapFile)
if err != nil {
fmt.Println(err)
standby()
return
}
tasmotalist := FindTasmotaDevices(FindPotentialHosts(nmapOutput.Hosts))
spin.Stop()
if len(tasmotalist) == 0 {
fmt.Println("nothing found.")
standby()
return
}
sort.SliceStable(tasmotalist, func(i, j int) bool {
return tasmotalist[i].Status.DeviceName > tasmotalist[j].Status.DeviceName
})
//find maximum length of device name
var (
namelens []int
verlens []int
)
for _, tasmota := range tasmotalist {
namelens = append(namelens, len(tasmota.Status.DeviceName))
verlens = append(verlens, len(tasmota.StatusFWR.Version))
}
sort.Ints(namelens)
sort.Sort(sort.Reverse(sort.IntSlice(namelens)))
sort.Ints(verlens)
sort.Sort(sort.Reverse(sort.IntSlice(verlens)))
padding := []int{len("255.255.255.255"), namelens[0], verlens[0]}
results := fmt.Sprintf("%-*s %-*s %-*s\n", padding[0], "IP Address", padding[1], "Device Name", padding[2], "Version")
for _, tasmota := range tasmotalist {
results += fmt.Sprintf("%-*s %-*s %-*s\n", padding[0], tasmota.StatusNET.IPAddress, padding[1], tasmota.Status.DeviceName, padding[2], tasmota.StatusFWR.Version)
}
fmt.Println(results)
fmt.Println("done.")
standby()
}