首页 > 解决方案 > SQLite, Python3

问题描述

我对 SQLite 有疑问。我有一个包含 5 个表的数据库。

表 1:扫描列:scan_id 和 scandate

表 2:系统列:system_id、ip、dns..

表 3:端口列:port_id、协议、端口、服务

表 4:插件列:plugin_id、PluginName、description

表 5:映射列:map_id、scan_id、system_id、port_id、plugin_id

所以一个系统可以有多个端口,一个端口可以注册一对多的插件。现在我想得到以下信息:最后一个扫描日

`system_IP, protocol, port, service and all plugins identified on that systems port` 

目前我被困在这个命令中:

`SELECT system_IP, protocol, port, service, PluginName FROM maps
 INNER JOIN scans ON (maps.scan_id = scans.scan_id)
 INNER JOIN systems ON (maps.system_id = systems.system_id)
 INNER JOIN ports ON (maps.ports_id = ports.ports_id)
 INNER JOIN plugins ON (maps.plugin_id = plugins.plugin_id)
 WHERE scan_id=1`

结果是:

`123.456.789.1, TCP, 22, SSH, Plugin1
 123.456.789.1, TCP, 80, HTTP, Plugin2
 123.456.789.1, TCP, 80, HTTP, Plugin3`

我想要的是以下内容:

`123.456.789.1, TCP, 22, SSH, Plugin1
 123.456.789.1, TCP, 80, HTTP, Plugin2, Plugin3, Plugin4
 123.456.789.1, TCP, 443, SSH, Plugin3`

我怎样才能做到这一点?

编辑:样本数据:

Table 1: scans
scan_id     |    scan_date
1                2.2020
2                3.2020

Table 2: systems
system_id   |    ip           |   dns
1             123.456.789.1     one.dns.com
2             123.456.789.2     two.dns.com

Table 3: ports
port_id     |   protocol      |   port    |  service
1               tcp               22         ssh
2               udp               161        smtp
3               tcp               80         http

Table 4: plugins
plugin_id    |  pluginname     |  plugindescription
1                1234567890       Beschreibung1
2                2345678901       Beschreibung2

Table 5: maps
maps_id      |  system_id      |   port_id   |   plugin_id | scan_id
1                  1                  1              1          1
2                  1                  3              1          1
3                  1                  3              2          1
4                  1                  3              3          1
5                  2                  2              1          1
6                  2                  2              2          1
7                  2                  2              3          1
8                  1                  1              1          2
...

希望这有助于澄清。

标签: sqlsqlite

解决方案


现在我想得到以下信息:最后一个扫描日

这表明类似:

SELECT system_IP, protocol, p.port, service, pl.PluginName
FROM (SELECT s.*
      FROM scans s
      ORDER BY scandate DESC
      LIMIT 1
     ) s JOIN
     maps m
     ON m.scan_id = s.scan_id JOIN
     systems sy
     ON m.system_id = sy.system_id JOIN
     ports p
     ON m.ports_id = p.ports_id JOIN
     plugins pl
     ON m.plugin_id = pl.plugin_id;

推荐阅读