Skip to content

Instantly share code, notes, and snippets.

@heemoe
Created January 18, 2017 12:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save heemoe/a42957c8dc636bd920935b9983c4d95d to your computer and use it in GitHub Desktop.
Save heemoe/a42957c8dc636bd920935b9983c4d95d to your computer and use it in GitHub Desktop.
一个将hosts文件转换成bind9 zone文件的脚本
#!/usr/bin/python3
import re
import datetime
import os
#download hosts file path.
hostsFile="hosts"
#zone file save path.
outputZoneFile="hosts.zone"
#target named zone file path
targetZoneFile="/usr/local/named/var/rpz.zone"
nameServer="ns1"
# I don't know what the means of following fields. Just copied from others.
domainName="pandadns.com"
# zoneSerial=datetime.datetime.strftime(datetime.datetime.now(), '%Y%m%d%H%M%S')
zoneSerial="2015112401"
refresh = "3H"
retry = "15M"
expiry = "1W"
SOA = [ ''+zoneSerial+' ; serial',
'3H ; refresh',
'15M ; retry',
'1W ; expiry',
'1D) ; minimum'
]
# Variables
ipaddresses=list()
zones=dict()
def createZonesHeader():
zonesHeader = str("$TTL 1D\n"
"@ IN SOA "+nameServer+"."+domainName+". root."+domainName+".(\n"
""+SOA[0]+"\n"
""+SOA[1]+"\n"
""+SOA[2]+"\n"
""+SOA[3]+"\n"
""+SOA[4]+"\n"
"@ IN NS "+nameServer+"."+domainName+".\n"
)
return zonesHeader
os.system('rm hosts')
#your hosts file url. default save to current path.
os.system('wget https://coding.net/u/scaffrey/p/hosts/git/raw/master/hosts')
createZonesHeader()
FILE = open(hostsFile,'r')
WriteFILE = open(outputZoneFile,'w+')
WriteFILE.write(createZonesHeader())
# count = 0
for line in FILE:
tmp=line.rstrip('|n')
tmp=re.sub('\s+',' ',tmp)
tmp=tmp.split(' ')
ipaddress=tmp[0]
ipname=tmp[1]
# ipv6
if ":" in ipaddress: continue
# hosts comments replace to ;
oneZone = str()
try:
if '#' in line[0]:
# line = line.replace('#',';',1)
continue
oneZone = line
elif len(line) == 0 and len(line) == 1:
print('blank line')
oneZone = '\n'
else:
oneZone = ipname + ' IN A ' + ipaddress
except:
print('error line')
# output zone file
if len(oneZone) == 12 and not ';' in line:
# count += 1
# print(oneZone + str(count))
continue
WriteFILE.write(oneZone+'\n')
FILE.close()
WriteFILE.close()
os.system('echo Enter y to replace named file, Enter other finish')
getChar = raw_input()
if getChar == 'y':
os.system('echo ------ start replace named file -----')
os.system('mv' + ' ' + targetZoneFile + ' ' + targetZoneFile + '.bak')
os.system('mv' + ' ' + outputZoneFile + ' ' + targetZoneFile)
os.system('echo replace done.')
# os.system('service named restart')
os.system('rndc reload')
else:
os.system('echo process done. Please check your hosts.zone file.:')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment