Jun-26-2022, 05:15 PM
Hi @AlphaInc ,
I understand you have two problems:
You dit almost right reading the JSON file, but you missed a detail. Look at the file: it starts with "[" so it is a list. There is one comma, so the list contains two items. The items start with "{" so they are dictionaries. You want the IP-address, and you see these addresses have key "ip".
So to extract these adresses you should do someting like:
So you need to use the default ping on your computer. On Windows you should execute one ping with "ping /n 1 192.168.1.2". On Linux-like systems you need to use "-c" (count) instead of "/n". So you could define a function like this:
I understand you have two problems:
- You need to read a JSON file,
- You need to know if an IP address responds.
You dit almost right reading the JSON file, but you missed a detail. Look at the file: it starts with "[" so it is a list. There is one comma, so the list contains two items. The items start with "{" so they are dictionaries. You want the IP-address, and you see these addresses have key "ip".
So to extract these adresses you should do someting like:
import json with open("clients.json") as f: data = json.load(f) for i in data: print(i["ip"])Now for the ping(). You could use pythonping, but if I understand the manual right, you need to be superuser to run a script using pythonping. I consider this to be bad practice.
So you need to use the default ping on your computer. On Windows you should execute one ping with "ping /n 1 192.168.1.2". On Linux-like systems you need to use "-c" (count) instead of "/n". So you could define a function like this:
from subprocess import call, DEVNULL import platform def ping(host_or_ip: str) -> bool : """ping() executes: on windows: ping /n 1 host_or_ip on Linux or Mac: ping -c 1 host_or_ip Meaning: execute one ping to host_or_ip. Returns True if host_or_ip responded, else False. """ if platform.system().lower() == "windows": countoption = "/n" else: countoption = "-c" return not call(["ping", countoption, "1", host_or_ip], stdout=DEVNULL, stderr=DEVNULL)Please show us how you use these building blocks to create your program. Let us know if you run into troubles.