-
Notifications
You must be signed in to change notification settings - Fork 44
/
nmapdb.sql
57 lines (51 loc) · 1.63 KB
/
nmapdb.sql
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*
* nmapdb - Parse nmap's XML output files and insert them into an SQLite database
* Copyright (c) 2012 Patroklos Argyroudis <argp at domain census-labs.com>
*/
CREATE TABLE IF NOT EXISTS hosts (
ip VARCHAR(16) PRIMARY KEY NOT NULL,
mac VARCHAR(18),
hostname VARCHAR(129),
protocol VARCHAR(5) DEFAULT 'ipv4',
os_name TEXT,
os_family TEXT,
os_accuracy INTEGER,
os_gen TEXT,
last_update TIMESTAMP,
state VARCHAR(8) DEFAULT 'down',
mac_vendor TEXT,
whois TEXT
);
CREATE TABLE IF NOT EXISTS ports (
ip VARCHAR(16) NOT NULL,
port INTEGER NOT NULL,
protocol VARCHAR(4) NOT NULL,
name VARCHAR(33),
state VARCHAR(33) DEFAULT 'closed',
service TEXT,
info TEXT,
PRIMARY KEY (ip, port, protocol),
CONSTRAINT fk_ports_hosts FOREIGN KEY (ip) REFERENCES hosts(ip) ON DELETE CASCADE
);
CREATE TRIGGER IF NOT EXISTS fki_ports_hosts_ip
BEFORE INSERT ON ports
FOR EACH ROW BEGIN
SELECT CASE
WHEN ((SELECT ip FROM hosts WHERE ip = NEW.ip) IS NULL)
THEN RAISE(ABORT, 'insert on table "ports" violates foreign key constraint "fk_ports_hosts"')
END;
END;
CREATE TRIGGER IF NOT EXISTS fku_ports_hosts_ip
BEFORE UPDATE ON ports
FOR EACH ROW BEGIN
SELECT CASE
WHEN ((SELECT ip FROM hosts WHERE ip = NEW.ip) IS NULL)
THEN RAISE(ABORT, 'update on table "ports" violates foreign key constraint "fk_ports_hosts"')
END;
END;
CREATE TRIGGER IF NOT EXISTS fkd_ports_hosts_ip
BEFORE DELETE ON hosts
FOR EACH ROW BEGIN
DELETE from ports WHERE ip = OLD.ip;
END;
/* EOF */