Aug-05-2020, 09:31 AM
(Aug-05-2020, 09:18 AM)buran Wrote: Now, what you show is dict, but I will assume it's a string
spam = """{"172.217.23.131": [{'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '1', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl': '0', 'service': {'name': 'tcpmux', 'method': 'table', 'conf': '3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '2', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl': '0', 'service': {'name': 'compressnet', 'method': 'table', 'conf': '3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '3', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl': '0', 'service': {'name': 'compressnet', 'method': 'table', 'conf': '3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '4', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl': '0', 'service': {'name': 'unknown', 'method': 'table', 'conf': '3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '5', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl': '0', 'service': {'name': 'rje', 'method': 'table', 'conf': '3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '6', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl': '0', 'service': {'name': 'unknown', 'method': 'table', 'conf': '3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '7', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl': '0', 'service': {'name': 'echo', 'method': 'table', 'conf': '3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '8', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl': '0', 'service': {'name': 'unknown', 'method': 'table', 'conf': '3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '9', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl': '0', 'service': {'name': 'discard', 'method': 'table', 'conf': '3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '10', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl': '0', 'service': {'name': 'unknown', 'method': 'table', 'conf':'3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '11', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl': '0', 'service': {'name': 'systat', 'method': 'table', 'conf': '3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '12', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl': '0', 'service': {'name': 'unknown', 'method': 'table', 'conf': '3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '13', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl':'0', 'service': {'name': 'daytime', 'method': 'table', 'conf': '3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '14', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl': '0', 'service': {'name': 'unknown', 'method': 'table', 'conf': '3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '15', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl':'0', 'service': {'name': 'netstat', 'method': 'table', 'conf': '3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '16', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl': '0', 'service': {'name': 'unknown', 'method': 'table', 'conf': '3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '17', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl':'0', 'service': {'name': 'qotd', 'method': 'table', 'conf': '3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '18', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl': '0', 'service': {'name': 'msp', 'method': 'table', 'conf': '3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '19', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl': '0', 'service': {'name': 'chargen', 'method': 'table', 'conf': '3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '20', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl': '0', 'service': {'name': 'ftp-data', 'method': 'table', 'conf': '3'}},{'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '21', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl': '0', 'service': {'name': 'ftp', 'method': 'table', 'conf': '3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '22', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl': '0', 'service': {'name': 'ssh', 'method': 'table', 'conf': '3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '23', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl': '0', 'service': {'name': 'telnet', 'method': 'table', 'conf': '3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '24', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl': '0', 'service': {'name': 'priv-mail', 'method': 'table', 'conf': '3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '25', 'state': 'open', 'reason': 'syn-ack', 'reason_ttl': '0', 'service': {'name': 'smtp', 'method': 'table', 'conf': '3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '26', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl': '0', 'service': {'name': 'rsftp', 'method': 'table', 'conf': '3'}}]}""" from ast import literal_eval eggs = literal_eval(spam) for element in eggs.values(): for item in element: for key, value in item.items(): print(f'{key} --> {value}') print('\n-----------------------\n\n') print('\n================================\n\n')Output
Using ast.literal_eval is safe way (in comparison to just useeval
) to convert the string to dict
In addition you can look at https://pypi.org/project/python3-nmap/ that may be more convenient and will yield json in which case you can use json module to parse it further
Thank you it works very well it's great