Reputation: 151
this is my code:
the "constants"
import os
import tempfile
import shutil
file_domini = open("ns_list.txt", "r") #contains a list of ns
dir_name = '/var/named/'
filename_suffix = '.zone'
bad_words = ['TTL']
prep_str = "$TTL 1d"
3 functions:
def line_prepender(filename, string): #add a string on file top
with open(filename, 'r+') as f:
content = f.read()
f.seek(0, 0)
f.write(string.rstrip('\r\n') + '\n' + content)
def create_temporary_copy(path): #create a copy of path using temp files
temp_dir = tempfile.gettempdir()
temp_path = os.path.join(temp_dir, 'zone_file.tmp')
shutil.copy2(path, temp_path)
return temp_path
def zone_file_checker(percorso_file): #add/edit/check some condition of zone file
file_di_zona = open(filename, 'r') #"file di zona" means zone file
text = file_di_zona.read()
# copy file_di_zona on tmp and remove lines containing $TTL
# occorrenze di TTL
with open(create_temporary_copy(filename), 'r+') as tmp:
for line in text:
# search and remove eventually wrong TTL strings
if not any(bad_word in line for bad_word in bad_words):
tmp.write(line)
# add $TTL 1d in prepending
line_prepender(tmp, prep_str)
#continue
#continue
#this 4 lines
# add @ before mx records
if line.lstrip().startswith('IN') and 'MX' in line:
line = "@" + line[1:]
tmp.write(line)
print tmp
tmp.close()
file_di_zona.close()
for riga in file_domini: #"riga" means "line"
# modifico casi particolari
if not riga.startswith('('):
# replace to fit file name syntax
zone_name = riga.replace('.', '_').replace('\n', '')
filename = dir_name + zone_name + filename_suffix
print filename
count = 0
try:
zone_file_checker(filename)
except IOError as e:
count += 1
# print filename + ":"
print " I/O error({0}): {1}".format(e.errno, e.strerror)
if count > 0:
print "IOError: %d domain names not found" % count
file_domini.close()
as you can see in the comment in the third subfunction, at the
# add @ before mx records
if line.lstrip().startswith('IN') and 'MX' in line: #the main problem
line = "@" + line[1:]
tmp.write(line)
it doesn't write on file.
this is a sample of zone file:
IN MX 20 mail2.example.com.
IN MX 50 mail3
example.com. IN A 192.0.2.1
IN AAAA 2001:db8:10::1
ns IN A 192.0.2.2
IN AAAA 2001:db8:10::2
and this is a sample of ns_list.txt
example1.com
( dns.example.it. )
( dns1.example.it. )
example2.eu
( dns1.example.it. )
( dns.example.it. )
example3.mobi
( dns2.example.it. )
( dns.example.it. )
example4.com
( dns1.novanet.it. )
( dns.novanet.it. )
there are some files named sitename_com.zone, sitename_it.zone, sitename_ext.zone, corrisponding to each domain (example1.com, example2.eu, ecc)
the main thing I need to fix is that i can not either write on a copy(temp) of these files or directly on them.
Upvotes: 1
Views: 320
Reputation: 151
I edited some line of code now works fine
def zone_file_checker(filename):
zone_file = open(filename, 'r')
# copio file_di_zona su tmp, e rimuovo righe di eventuali
# occorrenze di TTL
with open(create_temporary_copy(filename), 'r+') as tmp:
line_prepender(tmp, prep_str)
# print("byte #" + str(tmp.tell()))
for line in zone_file: # if you put tmp it doesn't print @ before mx
# vedi context manager e any
if not '$TTL' in line:
# aggiungo @ davanti agli mx records
if line.lstrip().startswith('IN') and 'MX' in line:
line = "@" + line[1:]
tmp.write(line)
else:
tmp.write(line)
tmp.seek(0, 0)
# print(tmp.tell())
print(tmp.read())
tmp.close()
zone_file.close()
and the prepender function
def line_prepender(filename, stringa):
filename.seek(0, 0)
filename.writelines([stringa.rstrip('\r\n') + '\n'])
Upvotes: 0
Reputation: 8702
def zone_file_checker(filename):
""" aggiunge, modifica e verifica
le condizioni per cui il file di zona
sia corretto e non dia problemi
in fase di migrazione"""
text = open(filename, 'r')
# copio file_di_zona su tmp, e rimuovo righe di eventuali
# occorrenze di TTL
with open(create_temporary_copy(filename), 'r+') as tmp:
for line in text:
# vedi context manager e any
if not any(bad_word in line for bad_word in bad_words):
if line.lstrip().startswith('IN') and 'MX' in line:
line = "@" + line[1:]
tmp.write(line)
else:
tmp.write(line)
# aggiungo $TTL 1d in prepending
# line_prepender(tmp, prep_str)
# aggiungo @ davanti agli mx records
# esco
print tmp
tmp.close()
text.close()
u made 2 mistakes
1)
file_di_zona = open(filename, 'r') #"file di zona" means zone file
text = file_di_zona.read()
solution use below line dont read. :
text = open(filename, 'r')
2)two ifs for bad words and line
if not any(bad_word in line for bad_word in bad_words):
if line.lstrip().startswith('IN') and 'MX' in line:
line = "@" + line[1:]
tmp.write(line)
else:
tmp.write(line)
last i dont know why r u using line prepender . dont use it.
Upvotes: 1
Reputation: 7923
In a call to
line_prepender(tmp, prep_str)
tmp
is not a filename, as line_prepender
expects. This is one possible place where things start to fall apart.
Upvotes: 0