-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathget_leases.go
40 lines (38 loc) · 1.7 KB
/
get_leases.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package main
import (
"fmt"
_ "github.com/go-sql-driver/mysql"
"database/sql"
"os"
"strings"
)
func get_lease_expirations(dbtype string, constring string) {
var line []string
fmt.Println("Connecting to database: kea, table: lease4...")
db, err := sql.Open(dbtype, constring)
defer db.Close()
outFile, err := os.Create("active-leases.txt")
defer outFile.Close()
//results, err := db.Query("SELECT INET_NTOA(ipv4_address), HEX(dhcp_identifier), dhcp4_subnet_id, hostname FROM lease4")
results, err := db.Query("SELECT INET_NTOA(address),HEX(hwaddr),expire,subnet_id,hostname from lease4 order by expire asc")
if err != nil {
panic(err.Error())
}
fmt.Println("Dumping active leases to active-leases.txt...")
for results.Next() {
var tag kea_lease_entry
if err := results.Scan(&tag.address, &tag.hwaddr, &tag.expire, &tag.subnet_id, &tag.hostname); err != nil {
panic(err)
}
//fmt.Println("-----------------------------------------------------------------------------------")
//fmt.Println("|", tag.address,"|",tag.hwaddr,"|",tag.expire,"|",tag.subnet_id,"|",tag.hostname,"|")
//fmt.Println("-----------------------------------------------------------------------------------")
line = []string{tag.address,tag.hwaddr,tag.expire,tag.subnet_id,tag.hostname}
linestr := strings.Join(line,",")
outFile.WriteString(linestr)
outFile.WriteString("\n")
rows++
}
fmt.Println("Records returned: ", rows, "(are actives leases with known expirations)")
db.Close()
}