-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
from pony.orm import Database, db_session | ||
from pony.orm.core import Entity, Optional, PrimaryKey, Required, Set, commit, sql_debug | ||
from datetime import datetime | ||
|
||
from pony.orm.ormtypes import StrArray | ||
from lib.files import LOCAL_DIR | ||
|
||
DB_PATH = LOCAL_DIR / 'db.sqlite' | ||
|
||
db = Database() | ||
|
||
|
||
class Target(db.Entity): | ||
ip = PrimaryKey(str) | ||
created_at = Required(datetime, default=datetime.now()) | ||
updated_at = Required(datetime, default=datetime.now()) | ||
ports = Set('Port') | ||
comment = Optional(str) | ||
|
||
def before_update(self): | ||
self.updated_at = datetime.now() | ||
|
||
|
||
class Port(db.Entity): | ||
num = Required(int) | ||
comment = Optional(str) | ||
targets = Set(Target) | ||
tags = Optional(StrArray) | ||
|
||
|
||
@db_session | ||
def add_result(ip, port, comment='', tags=None): | ||
try: | ||
if tags is None: | ||
tags = [] | ||
t = Target.get(ip=ip) | ||
if not t: | ||
t = Target(ip=ip) | ||
if port not in t.ports.num: | ||
t.ports.add(Port(num=port, comment=comment)) | ||
for p in t.ports: | ||
if p.num == port: | ||
for tag in tags: | ||
p.tags.append(tag) | ||
t.updated_at = datetime.now() | ||
except Exception as e: | ||
print('error', repr(e)) | ||
|
||
|
||
db.bind(provider='sqlite', filename=str(DB_PATH), create_db=True) | ||
db.generate_mapping(create_tables=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ av | |
colorama | ||
fire | ||
pillow | ||
pony | ||
pyyaml | ||
requests | ||
tqdm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#!/usr/bin/env python3 | ||
from fire import Fire | ||
from pony.orm.core import select | ||
from pony.utils.utils import count | ||
from lib.models import Port, Target, db_session | ||
|
||
|
||
@db_session | ||
def main(): | ||
select((p.num, p.tags, count(p)) for p in Port).show() | ||
|
||
|
||
if __name__ == "__main__": | ||
Fire(main) |