import shutil # dhcp-host=c5,10.0.3.100 # dhcp-host=c6,10.0.3.101 find = { "ip": "10.0.3.100", "host": "c5" } to_write = [] # if find['ip'] in open("dnsmasq.conf").read(): # print "true" # for i in open("dnsmasq.conf", "r+"): # for i in open("dnsmasq.conf", "r"): # i = i.rstrip() # # string = i.split(",") # # # # host = string[0].split("=") # # # # if host[1] == find['host'] or string[1] == find['ip']: # # print "fail" # # # print host[1], string[1] # if find['ip'] in i: # print " x %s" % i # else: # print " v %s" % i # to_write.append(i) def dnsmasq_add(host, ip): print "read config" origin = [] clean = [] new = [] for i in open("dnsmasq.conf", "r"): origin.append(i.rstrip()) print "ip duplicates" for i in origin: if find['ip'] in i: print "delete ip: %s" % find['ip'] else: clean.append(i) print "host duplicates" for i in clean: if find['host'] in i: print "delete host: %s" % find['host'] else: new.append(i) f = open("dnsmasq.temp.conf", 'w') f.writelines(new) f.close() shutil.copyfile("dnsmasq.temp.conf", "dnsmasq.conf") print new