【发布时间】:2021-09-13 08:27:11
【问题描述】:
有一段python代码:
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))
我们得到输出(指定部分):
[
{
"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"
}
}
]
如何在列中只显示网站列表(不包括括号、主机、主机 ID、\n 等)?
【问题讨论】:
-
类似于
[clean(host["inventory"]["site_notes"]) for host in hosts],其中clean是您需要创建的函数以删除不需要的字符串部分(例如_、\n、数字等,考虑到您自己的数据布局,由您来定义)