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.Hw = p[2]
|
||||
oneHost.Date = currentTime.Format("2006-01-02 15:04:05")
|
||||
oneHost.Now = 1
|
||||
foundHosts = append(foundHosts, oneHost)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
//"fmt"
|
||||
"log"
|
||||
"database/sql"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
|
|
@ -31,7 +31,7 @@ func db_select(dbPath string) (dbHosts []Host) {
|
|||
dbHosts = []Host{}
|
||||
for res.Next() {
|
||||
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 {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
|
@ -39,6 +39,6 @@ func db_select(dbPath string) (dbHosts []Host) {
|
|||
dbHosts = append(dbHosts, oneHost)
|
||||
}
|
||||
|
||||
fmt.Println("Select all:", dbHosts)
|
||||
//fmt.Println("Select all:", dbHosts)
|
||||
return dbHosts
|
||||
}
|
||||
|
|
@ -16,7 +16,8 @@ func db_create(dbPath string) {
|
|||
"MAC" TEXT,
|
||||
"HW" TEXT,
|
||||
"DATE" TEXT,
|
||||
"KNOWN" INTEGER DEFAULT 0
|
||||
"KNOWN" INTEGER DEFAULT 0,
|
||||
"NOW" INTEGER DEFAULT 0
|
||||
);`
|
||||
db_exec(dbPath, sqlStatement)
|
||||
fmt.Println("Table created!")
|
||||
|
|
@ -24,8 +25,8 @@ func db_create(dbPath string) {
|
|||
}
|
||||
|
||||
func db_insert(dbPath string, oneHost Host) {
|
||||
sqlStatement := `INSERT INTO "now" (NAME, IP, MAC, HW, DATE, KNOWN) VALUES ('','%s','%s','%s','%s', '%d');`
|
||||
sqlStatement = fmt.Sprintf(sqlStatement, oneHost.Ip, oneHost.Mac, oneHost.Hw, oneHost.Date, 1)
|
||||
fmt.Println("Insert statement:", sqlStatement)
|
||||
sqlStatement := `INSERT INTO "now" (NAME, IP, MAC, HW, DATE, KNOWN, NOW) VALUES ('%s','%s','%s','%s','%s','%d','%d');`
|
||||
sqlStatement = fmt.Sprintf(sqlStatement, oneHost.Name, oneHost.Ip, oneHost.Mac, oneHost.Hw, oneHost.Date, oneHost.Known, oneHost.Now)
|
||||
//fmt.Println("Insert statement:", sqlStatement)
|
||||
db_exec(dbPath, sqlStatement)
|
||||
}
|
||||
24
src/main.go
24
src/main.go
|
|
@ -22,18 +22,26 @@ type Conf struct {
|
|||
GuiPort string
|
||||
}
|
||||
|
||||
var AppConfig Conf
|
||||
|
||||
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_insert(newConfig.DbPath, foundHosts[0])
|
||||
db_select(newConfig.DbPath)
|
||||
db_create(AppConfig.DbPath)
|
||||
dbHosts := db_select(AppConfig.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>
|
||||
</tr>
|
||||
{{ range . }}
|
||||
<tr>
|
||||
<td>{{ .Name }}</td>
|
||||
<td>{{ .Ip }}</td>
|
||||
<td>{{ .Mac }}</td>
|
||||
<td>{{ .Hw }}</td>
|
||||
<td>{{ .Known }}</td>
|
||||
</tr>
|
||||
{{ if eq .Now 1 }}
|
||||
<tr>
|
||||
<td>{{ .Name }}</td>
|
||||
<td>{{ .Ip }}</td>
|
||||
<td>{{ .Mac }}</td>
|
||||
<td>{{ .Hw }}</td>
|
||||
<td>{{ .Known }}</td>
|
||||
</tr>
|
||||
{{ end }}
|
||||
{{ end }}
|
||||
</table>
|
||||
<h2>Log</h2>
|
||||
|
|
@ -34,14 +36,16 @@
|
|||
<th>Known</th>
|
||||
</tr>
|
||||
{{ range . }}
|
||||
<tr>
|
||||
<td>{{ .Name }}</td>
|
||||
<td>{{ .Ip }}</td>
|
||||
<td>{{ .Mac }}</td>
|
||||
<td>{{ .Hw }}</td>
|
||||
<td>{{ .Date }}</td>
|
||||
<td>{{ .Known }}</td>
|
||||
</tr>
|
||||
{{ if eq .Now 0 }}
|
||||
<tr>
|
||||
<td>{{ .Name }}</td>
|
||||
<td>{{ .Ip }}</td>
|
||||
<td>{{ .Mac }}</td>
|
||||
<td>{{ .Hw }}</td>
|
||||
<td>{{ .Date }}</td>
|
||||
<td>{{ .Known }}</td>
|
||||
</tr>
|
||||
{{ end }}
|
||||
{{ end }}
|
||||
</table>
|
||||
</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