Created
October 12, 2016 11:02
-
-
Save RPDiep/b5f596b53aa0c36810d6aebbc15d635c to your computer and use it in GitHub Desktop.
A wrapper around ping which uses ping6 if the target has an AAAA record
This file contains hidden or 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
#!/usr/bin/python3 | |
import ipaddr | |
import os | |
import socket | |
import sys | |
if len(sys.argv) == 1: | |
print("""Usage: %s <hostname|ip>""" % sys.argv[0]) | |
sys.exit(1) | |
else: | |
host = sys.argv[1] | |
try: | |
ip = socket.getaddrinfo(host, None)[0][4][0] | |
version = ipaddr.IPAddress(ip).version | |
if version == 4: | |
os.execl("/bin/ping", "/bin/ping", host) | |
elif version == 6: | |
os.execl("/bin/ping6", "/bin/ping6", host) | |
except: | |
print("unknown host") | |
sys.exit(2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Requires ipaddr.
Under ubuntu, install
python3-ipaddr