Commit d99d660b authored by vindarel's avatar vindarel

--undo + check max servers in tail

parent ab87ef24
......@@ -22,15 +22,22 @@ Get the nearest dns servers and insert them into your configuration.
usage: sudo python
apt-get install resolvconf # installed in Ubuntu ?
Requirements (on Debian):
apt-get install resolvconf # installed in Ubuntu
apt-get install python-pip && pip install BeautifulSoup4
Full information:
DEBIAN_CONF = "/etc/resolvconf/resolv.conf.d/tail"
DIR_RESOLVCONF = "/etc/resolvconf/resolv.conf.d/"
# UBUNTU_CONF = "/etc/resolvconf/resolv.conf.d/tail"
# = working resolv.conf (overwriten)
RESOLVCONF = "/sbin/resolvconf"
DEFAULT_DNS = ["", "",] #UK and US
BACK_SUFFIX = ".opennicset-back"
# def getConf():
# """
......@@ -48,7 +55,7 @@ DEFAULT_DNS = ["", "",] #UK and US
def getDnsList():
"""Gets your nearest OpenNic servers from their page
"""Gets your nearest openNIC servers from their page
If this service isn't working, uses default servers.
......@@ -56,6 +63,7 @@ def getDnsList():
returns a list of IP addresses.
url = ""
print "Connecting to %s..." % (url,)
r = urllib2.urlopen(url)
nic = BeautifulSoup(
assert nic
......@@ -82,31 +90,52 @@ def testOpennic(url="http://wiki.opennic.glue/SponsoredTLDs"):
print "Testing… ",
ret = urllib2.urlopen(url)
if ret:
if not ret:
print "test SUCCESSFUL: we can access opennic's %s" % (url,)
return ret
return 0
print "test FAILED: we can't access opennic's domains (trying %s)" % (url,)
return 1
def maxServersExceeded(lines):
nb_servers = 0
for l in lines:
if l.startswith("nameserver"):
nb_servers += 1
return nb_servers >= MAX_SERVERS
def editConf(dns_list, conf):
"""Writes the given dns servers to the given file.
#TODO: if we have more than 3 nameservers they will be ignored.
BACK = conf + ".back"
back = conf + BACK_SUFFIX
if os.path.exists(conf):
if not os.path.exists(BACK):
shutil.copyfile(conf, BACK)
if not os.path.exists(back):
shutil.copyfile(conf, back)
print "Configuration file %s not found. Did you install the 'resolvconf' package ? (sudo apt-get install resolvconf)" % (conf,)
w = "# openNIC servers:\n".join("nameserver " + dns for dns in dns_list)
# resolvconf can be installed but …/tail not existing
base = conf.replace('/tail', '/base')
if os.path.exists(base):
# create and leave
open(conf, 'w').close()
print "%s file not found. Did you install resolvconf ?" % (conf,)
# if /tail already has more than 3 or 6? addresses, ours will be ignored (system limitation)
# Print a nice message.
with open(conf, 'r') as f:
lines = f.readlines()
if maxServersExceeded(lines):
print """There is more than %s servers in %s, so ours will be ignored. You can
still put one in /head""" % (MAX_SERVERS, conf)
w = " # openNIC\n".join("nameserver " + dns for dns in dns_list)
with open(conf, 'a+') as f:
print "we add the lines:\n" + w + " to the file " + conf
print "we saved the original conf file, so if you notice any connection pb, you can still put it back"
print "we add the lines:\n" + w + "\nto the file " + conf
print "We saved the original configuration file, so if you notice any connection pb, you can still put it back"
import ipdb; ipdb.set_trace()
def main(*args):
......@@ -122,14 +151,36 @@ def main(*args):
#todo: make possible to «undo»
def undo(conf_dir):
"""Put back the original configuration files."""
files = ["tail", "head"]
has_backup = False
for f in files:
f_path = conf_dir + f
back = conf_dir + f + BACK_SUFFIX
if os.path.exists(back):
has_backup = True
shutil.copyfile(back, f_path)
print "backup of %s restored." % (f_path,)
if not has_backup:
print "no backup found in %s" % (conf_dir,)
return 0
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Use an Opennic DNS server. Usage: sudo python")
parser.add_argument("-t", "--test", action='store_true',
help="test if we can access an opennic's TLD")
parser.add_argument("-u", "--undo", action='store_true',
help="put back the configuration files you had before starting playing with opennic-set.")
args = parser.parse_args()
if args.test:
if args.undo:
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment