为什么我的代码不将其从build_info函数收到的数据写入JSON文件?

发布于 2025-02-12 22:18:21 字数 1333 浏览 0 评论 0原文

api = shodan.Shodan(api_key)

query = 'MongoDB Server Information \n{ "process": "mongod" port:27017'

build_info_arr = []

try:
    results = api.search(query)
    print('Total Results: %s\n' % results['total'])

    for result in results['matches']:
        if "Authentication partially enabled" not in result['data']:
            print('IP: {}'.format(result['ip_str']))
            ip: str = format(result['ip_str'])
            collections = mongodb_search.build_info(ip)

            if collections:
                data = json.loads(collections)
                for build_infos in data.items():
                    build_info_arr.append(build_infos)

except shodan.APIError as e:
    print('Error: {}'.format(e))

with open('./test.json', "wt") as jsonfile:
    jsonfile.write(json.dumps(build_info_arr, indent=4, sort_keys=False))

我的build_info函数在这里:

def build_info(ip):
    try:
        client = pymongo.MongoClient(ip, 27017, maxPoolSize=10)
        data = ''
        for build_info in client.db.command({'buildInfo': 1}):
            data += str(json.dumps(build_info))
            file = json.loads(data)
            return file
    except:
        print('Error: Cannot retrieve buildinfo.')
api = shodan.Shodan(api_key)

query = 'MongoDB Server Information \n{ "process": "mongod" port:27017'

build_info_arr = []

try:
    results = api.search(query)
    print('Total Results: %s\n' % results['total'])

    for result in results['matches']:
        if "Authentication partially enabled" not in result['data']:
            print('IP: {}'.format(result['ip_str']))
            ip: str = format(result['ip_str'])
            collections = mongodb_search.build_info(ip)

            if collections:
                data = json.loads(collections)
                for build_infos in data.items():
                    build_info_arr.append(build_infos)

except shodan.APIError as e:
    print('Error: {}'.format(e))

with open('./test.json', "wt") as jsonfile:
    jsonfile.write(json.dumps(build_info_arr, indent=4, sort_keys=False))

My build_info function is here:

def build_info(ip):
    try:
        client = pymongo.MongoClient(ip, 27017, maxPoolSize=10)
        data = ''
        for build_info in client.db.command({'buildInfo': 1}):
            data += str(json.dumps(build_info))
            file = json.loads(data)
            return file
    except:
        print('Error: Cannot retrieve buildinfo.')

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

妄司 2025-02-19 22:18:21

您可以使用json.dump()而不是json.dumps()直接写入文件,请尝试更改以下:

with open('./test.json', "w") as jsonfile:
    json.dump(build_info_arr, jsonfile, indent=4, sort_keys=False)

You can use json.dump() instead of json.dumps() to writes the file directly, try changing to this:

with open('./test.json', "w") as jsonfile:
    json.dump(build_info_arr, jsonfile, indent=4, sort_keys=False)
变身佩奇 2025-02-19 22:18:21

与您的问题不是直接相关的,而是几件事:

  1. Shodan已经收集了信息并将其存储在mongodb.buildinfo属性中: https://datapedia.shodan.io/property/mongodb.html
  2. 您可以使用“ product:mongodb”的搜索查询,该查询较短,并利用我们的指纹,以便您可以在非非指纹上找到服务 - 标准端口。
  3. 使用shodan.search_cursor(query)方法迭代结果。它为您处理分页。请参阅: https://help.shodan.io/guides/guides/guides/如何掉落data-with-api

Not directly related to your question but a few things:

  1. Shodan already collects that information and stores it in the mongodb.buildInfo property: https://datapedia.shodan.io/property/mongodb.html
  2. You can use a search query of "product:mongodb" which is shorter and takes advantage of our fingerprinting so you can find services on non-standard ports.
  3. Use the Shodan.search_cursor(query) method to iterate over results. It handles paging for you. See: https://help.shodan.io/guides/how-to-download-data-with-api
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文