Skip to content

Commit

Permalink
changes, fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason Talon committed May 25, 2022
1 parent 30c41b8 commit 8c34606
Show file tree
Hide file tree
Showing 7 changed files with 344 additions and 138 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea
bin
**/*.xml
**/*.xml
.DS_Store
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ build/mac/m1:
GOOS=darwin GOARCH=arm64 go build -o ./bin/mac/m1/tasmotalist

build/win/amd64:
GOOS=windows GOARCH=amd64 go build -o ./bin/win/amd64/tasmotalist
GOOS=windows GOARCH=amd64 go build -o ./bin/win/amd64/tasmotalist.exe

build/win/arm64:
GOOS=windows GOARCH=arm64 go build -o ./bin/win/arm64/tasmotalist
GOOS=windows GOARCH=arm64 go build -o ./bin/win/arm64/tasmotalist.exe

build:
rimraf bin
Expand Down
6 changes: 4 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ module github.com/jasontalon/tasmotalist

go 1.18

require github.com/PuerkitoBio/goquery v1.8.0
require (
github.com/PuerkitoBio/goquery v1.8.0
github.com/briandowns/spinner v1.18.1
)

require (
github.com/andybalholm/cascadia v1.3.1 // indirect
github.com/briandowns/spinner v1.18.1 // indirect
github.com/fatih/color v1.7.0 // indirect
github.com/mattn/go-colorable v0.1.2 // indirect
github.com/mattn/go-isatty v0.0.8 // indirect
Expand Down
106 changes: 82 additions & 24 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,75 +3,133 @@ package main
import (
"fmt"
"os"
"path"
"path/filepath"
"runtime"
"sort"
"strings"
"time"

"github.com/briandowns/spinner"
)

func main() {
var spin = spinner.New(spinner.CharSets[2], 100*time.Millisecond)

if !CheckNmap() {
panic("nmap not installed")
}
func standby() {
fmt.Println("Press Enter Key to exit.")
fmt.Scanln()
}

wd, err := os.Getwd()
func main() {
spin.Prefix = "Performing nmap check "

spin.Start()
nmapV, err := CheckNmap()
spin.Stop()
if err != nil {
panic(err)
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"

nmapFullPath := path.Join(wd, nmapFile)
nmapFile = filepath.Join(os.TempDir(), nmapFile)

currentIp, err := GetOutboundIP()

currentIp := GetOutboundIP().String()
if err != nil {
fmt.Println(err)
standby()
return
}

ipRange := currentIp[0:strings.LastIndex(currentIp, ".")] + ".0-255"
currentIpStr := currentIp.String()

spin := spinner.New(spinner.CharSets[2], 100*time.Millisecond)
ipRange := currentIpStr[0:strings.LastIndex(currentIpStr, ".")] + ".0-255"

spin.Prefix = "Scanning network " + ipRange + " "

spin.Start()

_, err = ExecuteCmd("nmap", "--script-timeout", "2", "-d", "-oX", nmapFile, "-p", "80", ipRange)
b, err := ExecuteCmd("nmap", "--unprivileged", "--script-timeout", "2", "-d", "-oX", nmapFile, "-p", "80", ipRange)
spin.Stop()
if err != nil {
panic(err)
fmt.Println(b, err)
standby()
return
}

if _, err := os.Stat(nmapFullPath); err != nil {
panic(err)
if _, err := os.Stat(nmapFile); err != nil {
fmt.Println(err)
standby()
return
}

spin.Prefix = "Looking for tasmota devices... "

spin.Start()

nmapOutput, err := GetNmapOutput(nmapFullPath)
nmapOutput, err := GetNmapOutput(nmapFile)

if err != nil {
panic(err)
fmt.Println(err)
standby()
return
}

devices := FindTasmotaDevices(FindPotentialHosts(nmapOutput.Hosts))

ResolveTasmotaDeviceName(devices)
tasmotalist := FindTasmotaDevices(FindPotentialHosts(nmapOutput.Hosts))

spin.Stop()

if len(devices) == 0 {
if len(tasmotalist) == 0 {
fmt.Println("nothing found.")
standby()
return
}

results := ""
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]}

for _, device := range devices {
results += fmt.Sprintf("%-15s %s\n", device.Address.Addr, device.Hostnames.Hostname.Name)
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()
}
134 changes: 134 additions & 0 deletions tasmota_info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
package main

type TasmotaInfo struct {
Warning string `json:"WARNING"`
Status struct {
Module int `json:"Module"`
DeviceName string `json:"DeviceName"`
FriendlyName []string `json:"FriendlyName"`
Topic string `json:"Topic"`
ButtonTopic string `json:"ButtonTopic"`
Power int `json:"Power"`
PowerOnState int `json:"PowerOnState"`
LedState int `json:"LedState"`
LedMask string `json:"LedMask"`
SaveData int `json:"SaveData"`
SaveState int `json:"SaveState"`
SwitchTopic string `json:"SwitchTopic"`
SwitchMode []int `json:"SwitchMode"`
ButtonRetain int `json:"ButtonRetain"`
SwitchRetain int `json:"SwitchRetain"`
SensorRetain int `json:"SensorRetain"`
PowerRetain int `json:"PowerRetain"`
InfoRetain int `json:"InfoRetain"`
StateRetain int `json:"StateRetain"`
} `json:"Status"`
StatusPRM struct {
Baudrate int `json:"Baudrate"`
SerialConfig string `json:"SerialConfig"`
GroupTopic string `json:"GroupTopic"`
OtaURL string `json:"OtaUrl"`
RestartReason string `json:"RestartReason"`
Uptime string `json:"Uptime"`
StartupUTC string `json:"StartupUTC"`
Sleep int `json:"Sleep"`
CfgHolder int `json:"CfgHolder"`
BootCount int `json:"BootCount"`
BCResetTime string `json:"BCResetTime"`
SaveCount int `json:"SaveCount"`
SaveAddress string `json:"SaveAddress"`
} `json:"StatusPRM"`
StatusFWR struct {
Version string `json:"Version"`
BuildDateTime string `json:"BuildDateTime"`
Boot int `json:"Boot"`
Core string `json:"Core"`
Sdk string `json:"SDK"`
CPUFrequency int `json:"CpuFrequency"`
Hardware string `json:"Hardware"`
Cr string `json:"CR"`
} `json:"StatusFWR"`
StatusLOG struct {
SerialLog int `json:"SerialLog"`
WebLog int `json:"WebLog"`
MqttLog int `json:"MqttLog"`
SysLog int `json:"SysLog"`
LogHost string `json:"LogHost"`
LogPort int `json:"LogPort"`
SSID []string `json:"SSId"`
TelePeriod int `json:"TelePeriod"`
Resolution string `json:"Resolution"`
SetOption []string `json:"SetOption"`
} `json:"StatusLOG"`
StatusMEM struct {
ProgramSize int `json:"ProgramSize"`
Free int `json:"Free"`
Heap int `json:"Heap"`
ProgramFlashSize int `json:"ProgramFlashSize"`
FlashSize int `json:"FlashSize"`
FlashChipID string `json:"FlashChipId"`
FlashFrequency int `json:"FlashFrequency"`
FlashMode int `json:"FlashMode"`
Features []string `json:"Features"`
Drivers string `json:"Drivers"`
Sensors string `json:"Sensors"`
} `json:"StatusMEM"`
StatusNET struct {
Hostname string `json:"Hostname"`
IPAddress string `json:"IPAddress"`
Gateway string `json:"Gateway"`
Subnetmask string `json:"Subnetmask"`
DNSServer1 string `json:"DNSServer1"`
DNSServer2 string `json:"DNSServer2"`
Mac string `json:"Mac"`
Webserver int `json:"Webserver"`
HTTPAPI int `json:"HTTP_API"`
WifiConfig int `json:"WifiConfig"`
WifiPower float64 `json:"WifiPower"`
} `json:"StatusNET"`
StatusMQT struct {
MqttHost string `json:"MqttHost"`
MqttPort int `json:"MqttPort"`
MqttClientMask string `json:"MqttClientMask"`
MqttClient string `json:"MqttClient"`
MqttUser string `json:"MqttUser"`
MqttCount int `json:"MqttCount"`
MaxPacketSize int `json:"MAX_PACKET_SIZE"`
Keepalive int `json:"KEEPALIVE"`
SocketTimeout int `json:"SOCKET_TIMEOUT"`
} `json:"StatusMQT"`
StatusTIM struct {
Utc string `json:"UTC"`
Local string `json:"Local"`
StartDST string `json:"StartDST"`
EndDST string `json:"EndDST"`
Timezone string `json:"Timezone"`
Sunrise string `json:"Sunrise"`
Sunset string `json:"Sunset"`
} `json:"StatusTIM"`
StatusSNS struct {
Time string `json:"Time"`
} `json:"StatusSNS"`
StatusSTS struct {
Time string `json:"Time"`
Uptime string `json:"Uptime"`
UptimeSec int `json:"UptimeSec"`
Heap int `json:"Heap"`
SleepMode string `json:"SleepMode"`
Sleep int `json:"Sleep"`
LoadAvg int `json:"LoadAvg"`
MqttCount int `json:"MqttCount"`
Power string `json:"POWER"`
Wifi struct {
Ap int `json:"AP"`
SSID string `json:"SSId"`
BSSID string `json:"BSSId"`
Channel int `json:"Channel"`
Mode string `json:"Mode"`
Rssi int `json:"RSSI"`
Signal int `json:"Signal"`
LinkCount int `json:"LinkCount"`
Downtime string `json:"Downtime"`
} `json:"Wifi"`
} `json:"StatusSTS"`
}
Loading

0 comments on commit 8c34606

Please sign in to comment.