separate now and log
This commit is contained in:
parent
3a53c0a652
commit
877c854261
6 changed files with 73 additions and 30 deletions
|
|
@ -32,6 +32,7 @@ func parse_output(text string) ([]Host) {
|
||||||
oneHost.Mac = p[1]
|
oneHost.Mac = p[1]
|
||||||
oneHost.Hw = p[2]
|
oneHost.Hw = p[2]
|
||||||
oneHost.Date = currentTime.Format("2006-01-02 15:04:05")
|
oneHost.Date = currentTime.Format("2006-01-02 15:04:05")
|
||||||
|
oneHost.Now = 1
|
||||||
foundHosts = append(foundHosts, oneHost)
|
foundHosts = append(foundHosts, oneHost)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
//"fmt"
|
||||||
"log"
|
"log"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
_ "github.com/mattn/go-sqlite3"
|
_ "github.com/mattn/go-sqlite3"
|
||||||
|
|
@ -31,7 +31,7 @@ func db_select(dbPath string) (dbHosts []Host) {
|
||||||
dbHosts = []Host{}
|
dbHosts = []Host{}
|
||||||
for res.Next() {
|
for res.Next() {
|
||||||
var oneHost Host
|
var oneHost Host
|
||||||
err = res.Scan(&oneHost.Id, &oneHost.Name, &oneHost.Ip, &oneHost.Mac, &oneHost.Hw, &oneHost.Date, &oneHost.Known)
|
err = res.Scan(&oneHost.Id, &oneHost.Name, &oneHost.Ip, &oneHost.Mac, &oneHost.Hw, &oneHost.Date, &oneHost.Known, &oneHost.Now)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
@ -39,6 +39,6 @@ func db_select(dbPath string) (dbHosts []Host) {
|
||||||
dbHosts = append(dbHosts, oneHost)
|
dbHosts = append(dbHosts, oneHost)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println("Select all:", dbHosts)
|
//fmt.Println("Select all:", dbHosts)
|
||||||
return dbHosts
|
return dbHosts
|
||||||
}
|
}
|
||||||
|
|
@ -16,7 +16,8 @@ func db_create(dbPath string) {
|
||||||
"MAC" TEXT,
|
"MAC" TEXT,
|
||||||
"HW" TEXT,
|
"HW" TEXT,
|
||||||
"DATE" TEXT,
|
"DATE" TEXT,
|
||||||
"KNOWN" INTEGER DEFAULT 0
|
"KNOWN" INTEGER DEFAULT 0,
|
||||||
|
"NOW" INTEGER DEFAULT 0
|
||||||
);`
|
);`
|
||||||
db_exec(dbPath, sqlStatement)
|
db_exec(dbPath, sqlStatement)
|
||||||
fmt.Println("Table created!")
|
fmt.Println("Table created!")
|
||||||
|
|
@ -24,8 +25,8 @@ func db_create(dbPath string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func db_insert(dbPath string, oneHost Host) {
|
func db_insert(dbPath string, oneHost Host) {
|
||||||
sqlStatement := `INSERT INTO "now" (NAME, IP, MAC, HW, DATE, KNOWN) VALUES ('','%s','%s','%s','%s', '%d');`
|
sqlStatement := `INSERT INTO "now" (NAME, IP, MAC, HW, DATE, KNOWN, NOW) VALUES ('%s','%s','%s','%s','%s','%d','%d');`
|
||||||
sqlStatement = fmt.Sprintf(sqlStatement, oneHost.Ip, oneHost.Mac, oneHost.Hw, oneHost.Date, 1)
|
sqlStatement = fmt.Sprintf(sqlStatement, oneHost.Name, oneHost.Ip, oneHost.Mac, oneHost.Hw, oneHost.Date, oneHost.Known, oneHost.Now)
|
||||||
fmt.Println("Insert statement:", sqlStatement)
|
//fmt.Println("Insert statement:", sqlStatement)
|
||||||
db_exec(dbPath, sqlStatement)
|
db_exec(dbPath, sqlStatement)
|
||||||
}
|
}
|
||||||
24
src/main.go
24
src/main.go
|
|
@ -22,18 +22,26 @@ type Conf struct {
|
||||||
GuiPort string
|
GuiPort string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var AppConfig Conf
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
newConfig := get_config("./data/config")
|
AppConfig = get_config("./data/config")
|
||||||
|
|
||||||
foundHosts := parse_output(scan_iface(newConfig.Iface))
|
foundHosts := parse_output(scan_iface(AppConfig.Iface))
|
||||||
|
|
||||||
fmt.Println("Found hosts:", foundHosts)
|
//fmt.Println("Found hosts:", foundHosts)
|
||||||
|
|
||||||
db_create(newConfig.DbPath)
|
db_create(AppConfig.DbPath)
|
||||||
db_insert(newConfig.DbPath, foundHosts[0])
|
dbHosts := db_select(AppConfig.DbPath)
|
||||||
db_select(newConfig.DbPath)
|
|
||||||
|
|
||||||
fmt.Println(fmt.Sprintf("http://%s:%s", newConfig.GuiIP, newConfig.GuiPort))
|
//fmt.Println("DB hosts:", dbHosts)
|
||||||
|
|
||||||
|
db_compare(foundHosts, dbHosts)
|
||||||
|
|
||||||
|
dbHosts = db_select(AppConfig.DbPath)
|
||||||
|
allHosts := append(foundHosts,dbHosts...)
|
||||||
|
|
||||||
|
fmt.Println(fmt.Sprintf("http://%s:%s", AppConfig.GuiIP, AppConfig.GuiPort))
|
||||||
|
|
||||||
webgui(newConfig, foundHosts)
|
webgui(AppConfig, allHosts)
|
||||||
}
|
}
|
||||||
|
|
@ -14,13 +14,15 @@
|
||||||
<th>Known</th>
|
<th>Known</th>
|
||||||
</tr>
|
</tr>
|
||||||
{{ range . }}
|
{{ range . }}
|
||||||
<tr>
|
{{ if eq .Now 1 }}
|
||||||
<td>{{ .Name }}</td>
|
<tr>
|
||||||
<td>{{ .Ip }}</td>
|
<td>{{ .Name }}</td>
|
||||||
<td>{{ .Mac }}</td>
|
<td>{{ .Ip }}</td>
|
||||||
<td>{{ .Hw }}</td>
|
<td>{{ .Mac }}</td>
|
||||||
<td>{{ .Known }}</td>
|
<td>{{ .Hw }}</td>
|
||||||
</tr>
|
<td>{{ .Known }}</td>
|
||||||
|
</tr>
|
||||||
|
{{ end }}
|
||||||
{{ end }}
|
{{ end }}
|
||||||
</table>
|
</table>
|
||||||
<h2>Log</h2>
|
<h2>Log</h2>
|
||||||
|
|
@ -34,14 +36,16 @@
|
||||||
<th>Known</th>
|
<th>Known</th>
|
||||||
</tr>
|
</tr>
|
||||||
{{ range . }}
|
{{ range . }}
|
||||||
<tr>
|
{{ if eq .Now 0 }}
|
||||||
<td>{{ .Name }}</td>
|
<tr>
|
||||||
<td>{{ .Ip }}</td>
|
<td>{{ .Name }}</td>
|
||||||
<td>{{ .Mac }}</td>
|
<td>{{ .Ip }}</td>
|
||||||
<td>{{ .Hw }}</td>
|
<td>{{ .Mac }}</td>
|
||||||
<td>{{ .Date }}</td>
|
<td>{{ .Hw }}</td>
|
||||||
<td>{{ .Known }}</td>
|
<td>{{ .Date }}</td>
|
||||||
</tr>
|
<td>{{ .Known }}</td>
|
||||||
|
</tr>
|
||||||
|
{{ end }}
|
||||||
{{ end }}
|
{{ end }}
|
||||||
</table>
|
</table>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
29
src/work.go
Normal file
29
src/work.go
Normal file
|
|
@ -0,0 +1,29 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
func host_in_db(host Host, dbHosts []Host) bool {
|
||||||
|
for _, oneHost := range dbHosts {
|
||||||
|
if host.Ip == oneHost.Ip {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func db_compare(foundHosts []Host, dbHosts []Host) {
|
||||||
|
fmt.Println("Found hosts:", foundHosts)
|
||||||
|
fmt.Println("DB hosts:", dbHosts)
|
||||||
|
|
||||||
|
for _, oneHost := range foundHosts {
|
||||||
|
if host_in_db(oneHost, dbHosts) {
|
||||||
|
fmt.Println("Host in db")
|
||||||
|
} else {
|
||||||
|
fmt.Println("No such host!")
|
||||||
|
oneHost.Now = 0
|
||||||
|
db_insert(AppConfig.DbPath, oneHost)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue