Skip to content

Commit

Permalink
Args for arp-scan
Browse files Browse the repository at this point in the history
  • Loading branch information
aceberg committed Aug 27, 2024
1 parent 9aa39c4 commit 1ab69ba
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 13 deletions.
24 changes: 18 additions & 6 deletions internal/arp/arpscan.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,27 @@ import (
"strings"
"time"

"github.com/aceberg/WatchYourLAN/internal/check"
"github.com/aceberg/WatchYourLAN/internal/models"
)

var arpArgs string

func scanIface(iface string) string {
cmd, err := exec.Command("arp-scan", "-glNx", "-I", iface).Output()
if err != nil {
var cmd *exec.Cmd

if arpArgs != "" {
cmd = exec.Command("arp-scan", "-glNx", arpArgs, "-I", iface)
} else {
cmd = exec.Command("arp-scan", "-glNx", "-I", iface)
}
out, err := cmd.Output()
slog.Debug(cmd.String())

if check.IfError(err) {
return string("")
}
return string(cmd)
return string(out)
}

func parseOutput(text, iface string) []models.Host {
Expand All @@ -40,16 +52,16 @@ func parseOutput(text, iface string) []models.Host {
}

// Scan all interfaces
func Scan(ifaces string) []models.Host {
func Scan(ifaces, args string) []models.Host {
var text string
var foundHosts = []models.Host{}
arpArgs = args

perString := strings.Split(ifaces, " ")

for _, iface := range perString {
text = scanIface(iface)

slog.Debug("Scanning interface " + iface)
text = scanIface(iface)
slog.Debug("Found IPs: \n" + text)

foundHosts = append(foundHosts, parseOutput(text, iface)...)
Expand Down
3 changes: 2 additions & 1 deletion internal/db/choose_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@ func SetCurrent(config models.Conf) {
currentDB.SQLitePath = config.DBPath
currentDB.PGConnect = config.PGConnect

if currentDB.Use == "postgres" {
if currentDB.Use == "postgres" && currentDB.PGConnect != "" {
currentDB.PrimaryKey = "BIGSERIAL PRIMARY KEY"
} else {
currentDB.Use = "sqlite"
currentDB.PrimaryKey = "INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE"
}

Expand Down
17 changes: 13 additions & 4 deletions internal/db/postgres.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package db

import (
// "log/slog"
"log/slog"

"github.com/jmoiron/sqlx"

Expand All @@ -17,7 +17,11 @@ func dbExecPG(sqlStatement string) {
// slog.Debug("PG Exec " + sqlStatement)

db, err := sqlx.Connect("postgres", currentDB.PGConnect)
check.IfError(err)
if check.IfError(err) {
slog.Warn("PostgreSQL connection error. Falling back to SQLite.")
currentDB.Use = "sqlite"
Create()
}
defer db.Close()

_, err = db.Exec(sqlStatement)
Expand All @@ -30,10 +34,15 @@ func selectPG(table string) (dbHosts []models.Host) {

// slog.Debug("PG Select " + sqlStatement)

db, _ := sqlx.Connect("postgres", currentDB.PGConnect)
db, err := sqlx.Connect("postgres", currentDB.PGConnect)
if check.IfError(err) {
slog.Warn("PostgreSQL connection error. Falling back to SQLite.")
currentDB.Use = "sqlite"
Create()
}
defer db.Close()

err := db.Select(&dbHosts, sqlStatement)
err = db.Select(&dbHosts, sqlStatement)
check.IfError(err)

return dbHosts
Expand Down
2 changes: 1 addition & 1 deletion internal/web/scan-routine.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func startScan(quit chan bool) {

if nowDate.After(plusDate) {

foundHosts = arp.Scan(appConfig.Ifaces)
foundHosts = arp.Scan(appConfig.Ifaces, appConfig.ArpArgs)
compareHosts(foundHosts)
allHosts = db.Select("now")

Expand Down
2 changes: 1 addition & 1 deletion internal/web/templates/config.html
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
<td><input name="timeout" type="number" class="form-control" value="{{ .Config.Timeout }}"></td>
</tr>
<tr>
<td>Args for arpscan</td>
<td>Args for arp-scan</td>
<td><input name="arpargs" type="text" class="form-control" value="{{ .Config.ArpArgs }}"></td>
</tr>
<tr>
Expand Down

0 comments on commit 1ab69ba

Please sign in to comment.