Last active
February 2, 2018 17:25
-
-
Save l1x/7140399e8ea020e98c490d2806a7d358 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
#!/usr/bin/env python | |
import sys, socket, logging | |
def check_dns(hostname): | |
try: | |
forward = socket.gethostbyname(hostname) | |
(reverze,_,[ip]) = socket.gethostbyaddr(forward) | |
is_ok = 'yes' if hostname==reverze else 'no' | |
return { 'hostname': hostname, 'forward4': forward, 'reverse': reverze, 'status': is_ok } | |
except socket.gaierror, e: | |
logging.error(e) | |
logging.error("Socket error, cannot resolve name or IP, check spelling or ") | |
return {} | |
def check_params(args): | |
logging.info("Checking hostname") | |
if len(args) == 1: | |
logging.info("No hostname has been specified, using localhost") | |
print check_dns('localhost') | |
sys.exit(0) | |
elif len(args) == 2: | |
logging.info("Hostname has been specified: " + args[1] ) | |
print check_dns(args[1]) | |
sys.exit(0) | |
else: | |
logging.error("Unknown param") | |
sys.exit(2) | |
def main(): | |
try: | |
logging.basicConfig(level=logging.INFO, | |
format='%(asctime)s %(levelname)-8s %(message)s', | |
datefmt='%a, %d %b %Y %H:%M:%S') | |
logging.info('Interpreter location: %s', sys.executable) | |
logging.info('sys.argv: ' + str(sys.argv)) | |
check_params(sys.argv) | |
except KeyboardInterrupt: | |
logging.info("Ctrl+c was pressed, exiting...") | |
sys.exit() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment