|
1
|
|
|
# -*- coding: utf-8 -*- |
|
2
|
|
|
# Copyright (C) 2017-2021 Greenbone Networks GmbH |
|
3
|
|
|
# |
|
4
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later |
|
5
|
|
|
# |
|
6
|
|
|
# This program is free software: you can redistribute it and/or modify |
|
7
|
|
|
# it under the terms of the GNU General Public License as published by |
|
8
|
|
|
# the Free Software Foundation, either version 3 of the License, or |
|
9
|
|
|
# (at your option) any later version. |
|
10
|
|
|
# |
|
11
|
|
|
# This program is distributed in the hope that it will be useful, |
|
12
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14
|
|
|
# GNU General Public License for more details. |
|
15
|
|
|
# |
|
16
|
|
|
# You should have received a copy of the GNU General Public License |
|
17
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
18
|
|
|
|
|
19
|
|
|
import csv |
|
20
|
|
|
import sys |
|
21
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
def check_args(args): |
|
24
|
|
|
len_args = len(args.script) - 1 |
|
25
|
|
|
if len_args != 1: |
|
26
|
|
|
message = """ |
|
27
|
|
|
This script reads asset data from a csv file and sync it with the gsm. |
|
28
|
|
|
It needs one parameters after the script name. |
|
29
|
|
|
|
|
30
|
|
|
1. <csv_file> - should contain a table of IP-addresses with an optional a comment |
|
31
|
|
|
|
|
32
|
|
|
Example: |
|
33
|
|
|
$ gvm-script --gmp-username name --gmp-password pass \ |
|
34
|
|
|
ssh --hostname <gsm> scripts/sync-assets.gmp.py <csv_file> |
|
35
|
|
|
""" |
|
36
|
|
|
print(message) |
|
37
|
|
|
sys.exit() |
|
38
|
|
|
|
|
39
|
|
|
|
|
40
|
|
|
def sync_assets(gmp, filename): |
|
41
|
|
|
with open(filename, newline='') as f: |
|
42
|
|
|
reader = csv.reader(f, delimiter=',', quotechar='|') |
|
43
|
|
|
for row in reader: |
|
44
|
|
|
if len(row) == 2: |
|
45
|
|
|
ip = row[0] |
|
46
|
|
|
comment = row[1] |
|
47
|
|
|
# print('%s %s %s %s' % (host, ip, contact, location)) |
|
48
|
|
|
|
|
49
|
|
|
# check if asset is already there |
|
50
|
|
|
ret = gmp.get_assets( |
|
51
|
|
|
asset_type=gmp.types.AssetType.HOST, filter='ip=%s' % ip |
|
52
|
|
|
) |
|
53
|
|
|
if ret.xpath('asset'): |
|
54
|
|
|
print('\nAsset with IP %s exist' % ip) |
|
55
|
|
|
asset_id = ret.xpath('asset/@id')[0] |
|
56
|
|
|
gmp.delete_asset(asset_id=asset_id) |
|
57
|
|
|
else: |
|
58
|
|
|
print('Asset with ip %s does not exist. Sync...' % ip) |
|
59
|
|
|
ret = gmp.create_host(name=ip, comment=comment) |
|
60
|
|
|
|
|
61
|
|
|
if 'OK' in ret.xpath('@status_text')[0]: |
|
62
|
|
|
print('Asset synced') |
|
63
|
|
|
|
|
64
|
|
|
|
|
65
|
|
|
def main(gmp, args): |
|
66
|
|
|
# pylint: disable=undefined-variable |
|
67
|
|
|
|
|
68
|
|
|
check_args(args) |
|
69
|
|
|
|
|
70
|
|
|
file = args.script[1] |
|
71
|
|
|
|
|
72
|
|
|
sync_assets(gmp, file) |
|
73
|
|
|
|
|
74
|
|
|
|
|
75
|
|
|
if __name__ == '__gmp__': |
|
76
|
|
|
main(gmp, args) |
|
|
|
|
|
|
77
|
|
|
|