Last active
December 13, 2021 16:56
-
-
Save luizanao/e94703486077f2041cd2f0f38803390b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
This hacky-hacky script checks what's the lowest latency IP for a given DNS resource | |
""" | |
import subprocess | |
COUNT = "3" | |
res = [] | |
hostname = "MY.DNSDOMAIN.COM" | |
result = subprocess.run(['dig', '+short', hostname], stdout=subprocess.PIPE) | |
ips = list(result.stdout.decode("utf-8").replace("\\n", " ").split()) | |
print(ips) | |
for host in ips: | |
ping = subprocess.run(['ping', '-c', COUNT, host], stdout=subprocess.PIPE) | |
if ping.returncode != 0: | |
# host is down | |
continue | |
ping_time = ping.stdout.decode("utf-8") | |
ping_time = ping_time.split("round-trip min/avg/max/stddev =")[1].split()[0] | |
# e.g: 123.775/133.417/144.147/8.352 | |
# 0: min | |
# 1: avg | |
# 2: max | |
# 3: stddev | |
ping_time = ping_time.split("/")[1] | |
# e.g: '143.135' | |
res.append((host,ping_time)) | |
_min = min(res, key=lambda x: float(x[1])) | |
print(_min[0]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment