-2

There is a python code:

    import json
    
    from pyzabbix import ZabbixAPI
    
    username = "user"
    password = "*************"
    servername = "https://<name of the site.com>/api_jsonrpc.php"
    
    zapi = ZabbixAPI(servername)
    zapi.session.verify = False
    zapi.login(username, password)
    
    hosts = zapi.host.get(
        output=["host"], groupids=[25, 41, 43], selectInventory=["site_notes"],
    )
    
    print(json.dumps(hosts, indent=4, sort_keys=True))

We get the output (specified part):

[
    {
        "host": "CR-20",
        "hostid": "10355",
        "inventory": {
            "site_notes": "_\n_\n128\n4112\nastronomyoracle.com\nastronomyoracle.com\nastrooracles.com\nastrooracles.com\nastro-stars.com\nastro-stars.com\ndwrf.justapi.cc\ndwrf.justapi.cc\ngrafana.justapi.cc\ngrafana.justapi.cc\nhoro.astrozens.com\nhoro.astrozens.com\njcontent.justservices.cc\njcontent.justservices.cc\njustcontent.services\njustcontent.services\njustfeeds.services\njustfeeds.services\njustinsight.services\njustinsight.services\njustservices.cc\njustservices.cc\njustsupport.services\njustsupport.services\njusttemplate.services\nln.astrozens.com\nln.astrozens.com\nln.trivia.buzz\nln.trivia.buzz\npush.justapi.cc\npush.justapi.cc\nsegments.justapi.cc\nsegments.justapi.cc\ns_hash_bucket_size\ns_hash_max_size\nsms.justapi.cc\nsms.justapi.cc\nstat.justapi.cc\nstat.justapi.cc\ntrk.justservices.cc\ntrk.justservices.cc\nuser.justcontent.services\nusers.justapi.cc\nusers.justapi.cc\nusers.justtemplate.services\nwww.astronomyoracle.com\nwww.astrooracles.com\nwww.astro-stars.com\nwww.dwrf.justapi.cc\nwww.grafana.justapi.cc\nwww.grafana.justapi.cc\nwww.horo.astrozens.com\nwww.jcontent.justservices.cc\nwww.jcontent.justservices.cc\nwww.justcontent.services\nwww.justcontent.services\nwww.justfeeds.services\nwww.justfeeds.services\nwww.justinsight.services\nwww.justinsight.services\nwww.justservices.cc\nwww.justservices.cc\nwww.justsupport.services\nwww.justsupport.services\nwww.justtemplate.services\nwww.ln.astrozens.com\nwww.ln.trivia.buzz\nwww.push.justapi.cc\nwww.push.justapi.cc\nwww.segments.justapi.cc\nwww.segments.justapi.cc\nwww.sms.justapi.cc\nwww.sms.justapi.cc\nwww.stat.justapi.cc\nwww.stat.justapi.cc\nwww.trk.justservices.cc\nwww.user.justcontent.services\nwww.users.justapi.cc\nwww.users.justapi.cc\nwww.users.justtemplate.services"
        }
    },
        {
        "host": "DO-08",
        "hostid": "10659",
        "inventory": {
            "site_notes": "_\nbeta.everydayhoroscopes.com\ncontent.everydayhoroscopes.com\ncontent.everydayhoroscopes.com\neveryday-astrology.com\neveryday-horoscopes.net\neverydayhoroscopes.com\neverydayhoroscopes.com\neverydayhoroscopes.org\neverydayzenastrology.com\nmoonboundhoroscopes.com\nregular-horoscopes.com\nregularastrology.com\nwww.beta.everydayhoroscopes.com\nwww.beta.everydayhoroscopes.com\nwww.content.everydayhoroscopes.com\nwww.content.everydayhoroscopes.com\nwww.everyday-astrology.com\nwww.everyday-horoscopes.net\nwww.everydayhoroscopes.com\nwww.everydayhoroscopes.com\nwww.everydayhoroscopes.org\nwww.everydayzenastrology.com\nwww.moonboundhoroscopes.com\nwww.regular-horoscopes.com\nwww.regularastrology.com"
        }
    }
]

How can I display only a list of sites in a column (without brackets, host, hostid, \n, etc.)?

hardr0m
  • 1
  • 1
  • something like `[clean(host["inventory"]["site_notes"]) for host in hosts]`, where `clean` is a function you need to create to remove the parts of string you don't need (like `_`, `\n`, numbers, etc, it's up to you to define, considering your own data layout) – Alexey S. Larionov Sep 13 '21 at 08:33

2 Answers2

1

Try the below. The idea is to analyze the field that contains the data (site_notes),split it and clean unwanted entries.

data = [
    {
        "host": "CR-20",
        "hostid": "10355",
        "inventory": {
            "site_notes": "_\n_\n128\n4112\nastronomyoracle.com\nastronomyoracle.com\nastrooracles.com\nastrooracles.com\nastro-stars.com\nastro-stars.com\ndwrf.justapi.cc\ndwrf.justapi.cc\ngrafana.justapi.cc\ngrafana.justapi.cc\nhoro.astrozens.com\nhoro.astrozens.com\njcontent.justservices.cc\njcontent.justservices.cc\njustcontent.services\njustcontent.services\njustfeeds.services\njustfeeds.services\njustinsight.services\njustinsight.services\njustservices.cc\njustservices.cc\njustsupport.services\njustsupport.services\njusttemplate.services\nln.astrozens.com\nln.astrozens.com\nln.trivia.buzz\nln.trivia.buzz\npush.justapi.cc\npush.justapi.cc\nsegments.justapi.cc\nsegments.justapi.cc\ns_hash_bucket_size\ns_hash_max_size\nsms.justapi.cc\nsms.justapi.cc\nstat.justapi.cc\nstat.justapi.cc\ntrk.justservices.cc\ntrk.justservices.cc\nuser.justcontent.services\nusers.justapi.cc\nusers.justapi.cc\nusers.justtemplate.services\nwww.astronomyoracle.com\nwww.astrooracles.com\nwww.astro-stars.com\nwww.dwrf.justapi.cc\nwww.grafana.justapi.cc\nwww.grafana.justapi.cc\nwww.horo.astrozens.com\nwww.jcontent.justservices.cc\nwww.jcontent.justservices.cc\nwww.justcontent.services\nwww.justcontent.services\nwww.justfeeds.services\nwww.justfeeds.services\nwww.justinsight.services\nwww.justinsight.services\nwww.justservices.cc\nwww.justservices.cc\nwww.justsupport.services\nwww.justsupport.services\nwww.justtemplate.services\nwww.ln.astrozens.com\nwww.ln.trivia.buzz\nwww.push.justapi.cc\nwww.push.justapi.cc\nwww.segments.justapi.cc\nwww.segments.justapi.cc\nwww.sms.justapi.cc\nwww.sms.justapi.cc\nwww.stat.justapi.cc\nwww.stat.justapi.cc\nwww.trk.justservices.cc\nwww.user.justcontent.services\nwww.users.justapi.cc\nwww.users.justapi.cc\nwww.users.justtemplate.services"
        }
    },
    {
        "host": "DO-08",
        "hostid": "10659",
        "inventory": {
            "site_notes": "_\nbeta.everydayhoroscopes.com\ncontent.everydayhoroscopes.com\ncontent.everydayhoroscopes.com\neveryday-astrology.com\neveryday-horoscopes.net\neverydayhoroscopes.com\neverydayhoroscopes.com\neverydayhoroscopes.org\neverydayzenastrology.com\nmoonboundhoroscopes.com\nregular-horoscopes.com\nregularastrology.com\nwww.beta.everydayhoroscopes.com\nwww.beta.everydayhoroscopes.com\nwww.content.everydayhoroscopes.com\nwww.content.everydayhoroscopes.com\nwww.everyday-astrology.com\nwww.everyday-horoscopes.net\nwww.everydayhoroscopes.com\nwww.everydayhoroscopes.com\nwww.everydayhoroscopes.org\nwww.everydayzenastrology.com\nwww.moonboundhoroscopes.com\nwww.regular-horoscopes.com\nwww.regularastrology.com"
        }
    }
]

for entry in data:
    temp = entry['inventory']['site_notes'].split('\n')
    sites = [x for x in temp if len(x) > 1 and not x[0].isdigit()]
    print(sites)

output

['astronomyoracle.com', 'astronomyoracle.com', 'astrooracles.com', 'astrooracles.com', 'astro-stars.com', 'astro-stars.com', 'dwrf.justapi.cc', 'dwrf.justapi.cc', 'grafana.justapi.cc', 'grafana.justapi.cc', 'horo.astrozens.com', 'horo.astrozens.com', 'jcontent.justservices.cc', 'jcontent.justservices.cc', 'justcontent.services', 'justcontent.services', 'justfeeds.services', 'justfeeds.services', 'justinsight.services', 'justinsight.services', 'justservices.cc', 'justservices.cc', 'justsupport.services', 'justsupport.services', 'justtemplate.services', 'ln.astrozens.com', 'ln.astrozens.com', 'ln.trivia.buzz', 'ln.trivia.buzz', 'push.justapi.cc', 'push.justapi.cc', 'segments.justapi.cc', 'segments.justapi.cc', 's_hash_bucket_size', 's_hash_max_size', 'sms.justapi.cc', 'sms.justapi.cc', 'stat.justapi.cc', 'stat.justapi.cc', 'trk.justservices.cc', 'trk.justservices.cc', 'user.justcontent.services', 'users.justapi.cc', 'users.justapi.cc', 'users.justtemplate.services', 'www.astronomyoracle.com', 'www.astrooracles.com', 'www.astro-stars.com', 'www.dwrf.justapi.cc', 'www.grafana.justapi.cc', 'www.grafana.justapi.cc', 'www.horo.astrozens.com', 'www.jcontent.justservices.cc', 'www.jcontent.justservices.cc', 'www.justcontent.services', 'www.justcontent.services', 'www.justfeeds.services', 'www.justfeeds.services', 'www.justinsight.services', 'www.justinsight.services', 'www.justservices.cc', 'www.justservices.cc', 'www.justsupport.services', 'www.justsupport.services', 'www.justtemplate.services', 'www.ln.astrozens.com', 'www.ln.trivia.buzz', 'www.push.justapi.cc', 'www.push.justapi.cc', 'www.segments.justapi.cc', 'www.segments.justapi.cc', 'www.sms.justapi.cc', 'www.sms.justapi.cc', 'www.stat.justapi.cc', 'www.stat.justapi.cc', 'www.trk.justservices.cc', 'www.user.justcontent.services', 'www.users.justapi.cc', 'www.users.justapi.cc', 'www.users.justtemplate.services']
['beta.everydayhoroscopes.com', 'content.everydayhoroscopes.com', 'content.everydayhoroscopes.com', 'everyday-astrology.com', 'everyday-horoscopes.net', 'everydayhoroscopes.com', 'everydayhoroscopes.com', 'everydayhoroscopes.org', 'everydayzenastrology.com', 'moonboundhoroscopes.com', 'regular-horoscopes.com', 'regularastrology.com', 'www.beta.everydayhoroscopes.com', 'www.beta.everydayhoroscopes.com', 'www.content.everydayhoroscopes.com', 'www.content.everydayhoroscopes.com', 'www.everyday-astrology.com', 'www.everyday-horoscopes.net', 'www.everydayhoroscopes.com', 'www.everydayhoroscopes.com', 'www.everydayhoroscopes.org', 'www.everydayzenastrology.com', 'www.moonboundhoroscopes.com', 'www.regular-horoscopes.com', 'www.regularastrology.com']
balderman
  • 22,927
  • 7
  • 34
  • 52
  • Tanks! In" data", the information may change and I have indicated a small output, but it is 10 times larger. I'll try to experiment. – hardr0m Sep 13 '21 at 08:52
  • A colleague helped a lot. Maybe someone needs it, I post the code that works. – hardr0m Sep 14 '21 at 12:33
0
    import json
    import re
    from pyzabbix import ZabbixAPI
    
    username = "user"
    password = "*************"
    servername = "https://<name of the site.com>/api_jsonrpc.php"
    
    zapi = ZabbixAPI(servername)
    zapi.session.verify = False
    zapi.login(username, password)
    ef fqdn(
        name: str, test = re.compile(
            r"^(?!-)[\w-]{1,63}(?<!-)$", re.A
        ).fullmatch
):
    temp = name.split(".")
    return (
        all(test(i) for i in temp) if len(name) < 255 and len(temp) > 1 else False
    )


print(
    *sorted(
        {
            host for _ in zapi.host.get(
                output = ["host"], groupids = [25, 41, 43], selectInventory = ["site_notes"]
            ) for host in _["inventory"]["site_notes"].split("\n") if fqdn(host)
        }
    ), sep = "\n"
)
hardr0m
  • 1
  • 1
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-ask). – Community Sep 14 '21 at 12:48