Jul-26-2022, 12:19 PM
(Jul-24-2022, 12:15 AM)Calli Wrote: Thanks for sharing this but what you have shared is completely the opposite of what i wanted done. For instance if I check this IP
118.31.106.59 is down!
It says that it's down but the fact that this is not down when you check using a specific port which in this case is 9200.
The host itself could ignore ICMP packets (standard on Windows).
Something between could filter out ICMP packets. Using ping is not a reliable method to detect if a host is reachable over network.
Then you shared this code:
import requests with open('ip_lst1.txt') as f: for ip in f: ip = ip.strip() print(ip) response = requests.get(f"http://{ip}:9200/_cat/indices") print(response.text.strip())You got this Exception:
requests.exceptions.ConnectionError
Then catch this exception:
import requests with open('ip_lst1.txt') as f: for ip in f: ip = ip.strip() print(ip) try: response = requests.get(f"http://{ip}:9200/_cat/indices") except requests.exceptions.ConnectionError: print("Connection error") else: print(response.text.strip())In addition, you could and should add a
timeout
. It happens very frequently, that a host answers veryyy slooooooow.import requests with open('ip_lst1.txt') as f: for ip in f: ip = ip.strip() print(ip) try: # with timeout of 5 seconds # if timeout happens -> requests.exceptions.ConnectionError is raised response = requests.get(f"http://{ip}:9200/_cat/indices", timeout=5) except requests.exceptions.ConnectionError: print("Connection error") else: print(response.text.strip())By the way,
f
should be fd
or another better recognizable name.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
All humans together. We don't need politicians!