Source code for named_redirect.skeleton
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
This is a skeleton file that can serve as a starting point for a Python
console script. To run this script uncomment the following line in the
entry_points section in setup.py:
[console_scripts]
fibonacci = named_redirect.skeleton:run
Then run `python setup.py install` which will install the command `fibonacci`
inside your current environment.
Besides console scripts, the header (i.e. until _logger...) of this file can
also be used as template for Python modules.
Note: This skeleton file can be safely removed if not needed!
"""
from __future__ import division, print_function, absolute_import
import argparse
import sys
import logging
#import urllib.request
import os
from named_redirect import __version__
__author__ = "Anish Mangal"
__copyright__ = "Anish Mangal"
__license__ = "gpl3"
_logger = logging.getLogger(__name__)
[docs]def check_internet():
"""Check for internet connectivity
Args:
Returns:
bool: Whether connected to the internet or not
"""
_logger.debug("Checking for internet connectivity")
response = 1
try:
#urllib.request.urlopen('http://216.58.192.142', timeout=5)
if not os.system("ping -c 1 8.8.8.8"):
_logger.debug("Internet connectivity present!")
return True
else:
_logger.debug("Internet connectivity absent!")
return False
except OSError as err:
_logger.error("An error occurred while trying to determine internet connectivity!")
return False
[docs]def restart_bind9():
"""Restart bind9 named service
Returns:
bool: Whether operation was successful or not
"""
_logger.debug("Restarting the named/bind9 systemd service")
try:
os.system("sudo systemctl restart bind9.service")
except OSError as err:
_logger.error("Could not restart bind9")
return False
_logger.debug("Restarted bind9 service")
return True
[docs]def parse_args(args):
"""Parse command line parameters
Args:
args ([str]): command line parameters as list of strings
Returns:
:obj:`argparse.Namespace`: command line parameters namespace
"""
parser = argparse.ArgumentParser(
description="Check for internet and reconfigure and restart bind9 accordingly")
parser.add_argument(
'--version',
action='version',
version='named_redirect {ver}'.format(ver=__version__))
parser.add_argument(
'-j',
'--jail',
dest="dns_jail_enabled",
help="1 for enabling dns jail, 0 for disabling dns jail",
type=int,
metavar="INT")
parser.add_argument(
'-v',
'--verbose',
dest="loglevel",
help="set loglevel to INFO",
action='store_const',
const=logging.INFO)
parser.add_argument(
'-vv',
'--very-verbose',
dest="loglevel",
help="set loglevel to DEBUG",
action='store_const',
const=logging.DEBUG)
return parser.parse_args(args)
[docs]def setup_logging(loglevel):
"""Setup basic logging
Args:
loglevel (int): minimum loglevel for emitting messages
"""
logformat = "[%(asctime)s] %(levelname)s:%(name)s:%(message)s"
logging.basicConfig(level=loglevel, stream=sys.stdout,
format=logformat, datefmt="%Y-%m-%d %H:%M:%S")
[docs]def main(args):
"""Main entry point allowing external calls
Args:
args ([str]): command line parameter list
"""
args = parse_args(args)
setup_logging(args.loglevel)
_logger.info("Reconfiguring DNS for this machine")
reconfigure_bind9(args.dns_jail_enabled)
_logger.info("Script ends here")
[docs]def run():
"""Entry point for console_scripts
"""
main(sys.argv[1:])
if __name__ == "__main__":
run()