-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathsync-hosts.gmp.py
63 lines (46 loc) · 1.75 KB
/
sync-hosts.gmp.py
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
58
59
60
61
62
63
# SPDX-FileCopyrightText: 2017-2021 Greenbone AG
#
# SPDX-License-Identifier: GPL-3.0-or-later
import csv
import sys
from argparse import Namespace
from gvm.protocols.gmp import Gmp
def check_args(args):
len_args = len(args.script) - 1
if len_args != 1:
message = """
This script reads host data from a csv file and sync it with the gsm.
It needs one parameters after the script name.
1. <csv_file> - should contain a table of IP-addresses with an
optional a comment
Example:
$ gvm-script --gmp-username name --gmp-password pass \
ssh --hostname <gsm> scripts/sync-hosts.gmp.py <csv_file>
"""
print(message)
sys.exit()
def sync_hosts(gmp, filename):
with open(filename, newline="", encoding="utf-8") as f:
reader = csv.reader(f, delimiter=",", quotechar="|")
for row in reader:
if len(row) == 2:
ip = row[0]
comment = row[1]
# check if host exists
ret = gmp.get_hosts(filter_string=f"ip={ip}")
if ret.xpath("host"):
print(f"\nAsset with IP {ip} exist")
host_id = ret.xpath("host/@id")[0]
gmp.delete_host(host_id=host_id)
else:
print(f"Asset with ip {ip} does not exist. Sync...")
ret = gmp.create_host(name=ip, comment=comment)
if "OK" in ret.xpath("@status_text")[0]:
print("Asset synced")
def main(gmp: Gmp, args: Namespace) -> None:
# pylint: disable=undefined-variable
check_args(args)
file = args.script[1]
sync_hosts(gmp, file)
if __name__ == "__gmp__":
main(gmp, args)