#! /usr/bin/python3
import sys
import subprocess
import os.path

__author__ = 'brock'

def disableIP6():
    sys_ctl_conf_file = '/etc/sysctl.conf'
    if not os.path.isfile(sys_ctl_conf_file):
        print('Missing {0}'.format(sys_ctl_conf_file))
        return
    grep_exec = '/bin/grep'
    grep_ret = subprocess.call([grep_exec,
                                '-q', 'disable_ipv6', sys_ctl_conf_file])
    if grep_ret == 0:
        print('IP6 already disabled in {0}'.format(sys_ctl_conf_file))
        return
    try:
        with open(sys_ctl_conf_file, 'a') as sys_ctl_hand:
            sys_ctl_hand.writelines(
                ['net.ipv6.conf.all.disable_ipv6 = 1\n',
                 'net.ipv6.conf.default.disable_ipv6 = 1\n',
                 'net.ipv6.conf.lo.disable_ipv6 = 1\n'])
        subprocess.call(['/sbin/sysctl','-p'])
    except PermissionError:
        print('Unable to write {0}. Did you forget to sudo?'.
              format(sys_ctl_conf_File))
        return
    except:
        print('Unexpected error:',sys.exc_info()[0])

if __name__ == '__main__':
    disableIP6()

