How in python 3.10 do I take ip addresses from a json file, convert to a list and ping them?

Question:

I’m a network engineer who is learning python. Recently I’ve been working with json files, reading data via python, but am struggling to figure out the method to convert IP address strings into a list and ping each. I thought that taking this approach would be a good way to see how python works in a networking enviroment.

The json file contents are:
[
{
"name": "Router02",
"host": "10.1.2.13"
}
{
"name": "Router03",
"host": "10.0.1.13"
}
]

import json
from pythonping import ping

with open ('credentials.json', 'r') as f:
    hosts = json.load(f)

for router in hosts:
    print(router['host'])
Host = (router['host'])
ping(Host, verbose=True)

With this code I can ping the first address, as I understand that this takes the json data and converts into a dictionary, which cannot have duplicate key values. Converting to a list of IP addresses is the way forward.

How do I open the json file and convert the contents of the file to a list? Ideally I want to list only the IP addresses.

I’ve been trying variations, cut code right back to this to see if the file open and converison to a list has been successful:

import json
from pythonping import ping

with open ('credentials.json', 'r') as f:
    hosts = json.loads('credentials.json')
    print (hosts)

Have tried subsituting loads with dumps to but keep getting traceback errors.

I expected the print output to show me the list contents.

Asked By: AndyC

||

Answers:

  1. Your JSON is malformed, its content should be
 [ { "name": "Router02", "host": "10.1.2.13" }, { "name": "Router03", "host": "10.0.1.13" } ]

(list entries should be separated by comma)

  1. You should read the JSON content from the file object
with open ('credentials.json', 'r') as f:
    hosts = json.load(f)
    print (hosts)
  1. You can get a list of hosts from a dictionary with a comprehension loop
with open ('credentials.json', 'r') as f:
    hosts = json.load(f)
    hosts_list = [entry['host'] for entry in hosts]
    print (hosts_list)

Your final code could look like this (assuming that your JSON is correct):

import json
from pythonping import ping

with open ('credentials.json', 'r') as f:
    hosts = [entry['host'] for entry in json.load(f)]

for host in hosts:
    ping(host, verbose=True)
Answered By: Kuroneko

Thanks for the feedback. I used notepad++ with the json viewer to sort out the file format,which resolved that issue.

To answer @jarmod the file name was odd as it started life as a credentials file but ended up being a list of hosts as I found my way around.

Also, to answer @DarkKnight, I’m running this on windows which doesn’t care about who runs as what! I did have that issue with ping when running python scripts on Ubuntu earlier though.

Answered By: AndyC
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.