From 7e8f5fc6faa5886f105cff67ee5a3d526b9e470f Mon Sep 17 00:00:00 2001 From: cos Date: Sun, 4 Jun 2023 18:32:34 +0200 Subject: Add support for hostname suffix Allows for having a host reachable over different interfaces, such as e.g. somehost and somehost-wifi. --- go.mod | 2 +- hisgle.go | 20 ++++++++++++++------ 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/go.mod b/go.mod index 3540981..ae305c9 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,6 @@ module netizen.se/hisgle go 1.14 require ( - github.com/mxk/go-sqlite v0.0.0-20140611214908-167da9432e1f + github.com/mattn/go-sqlite3 v1.14.17 github.com/pborman/getopt v0.0.0-20190409184431-ee0cd42419d3 ) diff --git a/hisgle.go b/hisgle.go index de01ce9..ea7a397 100644 --- a/hisgle.go +++ b/hisgle.go @@ -10,7 +10,7 @@ import ( "fmt" // _ "code.google.com/p/gosqlite/sqlite3" import ( "database/sql" - _ "github.com/mxk/go-sqlite/sqlite3" + _ "github.com/mattn/go-sqlite3" "github.com/pborman/getopt" ) @@ -40,26 +40,34 @@ func hosts(db *sql.DB, vlan int) { var ( ipv4 string name string + suffix *string alias string + hostpart string ) // CREATE TABLE devices (name, mac, interface); // CREATE TABLE addresses (mac, vlan, ipv4); - rows, err := db.Query("SELECT a.ipv4, d.name FROM addresses AS a JOIN " + - "devices AS d ON a.mac=d.mac WHERE vlan=?", vlan) + rows, err := db.Query("SELECT a.ipv4, d.name, d.suffix " + + "FROM addresses AS a JOIN devices AS d ON a.mac=d.mac WHERE vlan=?", + vlan) if err != nil { log.Fatal(err) } for rows.Next() { - err := rows.Scan(&ipv4, &name) + err := rows.Scan(&ipv4, &name, &suffix) if err != nil { log.Fatal(err) } - fmt.Print(ipv4, "\t", name) + if suffix != nil { + hostpart = name + "-" + *suffix + } else { + hostpart = name + } + fmt.Print(ipv4, "\t", hostpart) // CREATE TABLE aliases (alias, target); - rows, err := db.Query("SELECT alias FROM aliases WHERE target=?", name) + rows, err := db.Query("SELECT alias FROM aliases WHERE target=?", hostpart) if err != nil { log.Fatal(err) } -- cgit v1.2.3