Add record add and zone save functions to main.Zone

Added main.Zone.new_record()
Added main.Zone.new_soa()
Added main.Zone.save_file()
This commit is contained in:
minecraftchest1@outlook.com 2024-11-05 19:39:02 -06:00
parent 8611d10b1b
commit 73bcbc02d4
2 changed files with 16 additions and 3 deletions

12
main.py
View file

@ -45,6 +45,7 @@ class SOA(Record):
@dataclass @dataclass
class Zone: class Zone:
origin: str
records: list = field(default_factory=list) records: list = field(default_factory=list)
def __str__(self): def __str__(self):
@ -55,6 +56,16 @@ class Zone:
return zone return zone
def new_soa(self, mname: str = 'ns1.example.com', rname: str = 'admin.example.com', serial: int = int(time.time()), refresh: int = 86400, retry: int = 7200, expire: int = 15552000, ttl: int = 21700):
if mname[-1] != '.':
mname = mname + '.' + self.origin
self.add(SOA(mname=mname, rname=rname, serial=serial, refresh=refresh, retry=retry, expire=expire, ttl=ttl))
def new_record(self, name: str = '@', ttl: str = 3600, rtype: str = 'A', data: str = '0.0.0.0'):
if name[-1] != '.':
name = name + '.' + self.origin
self.add(name=name, ttl=ttl, rtype=rtype, data=data)
def add(self, record: Record): def add(self, record: Record):
self.records.append(record) self.records.append(record)
@ -62,5 +73,6 @@ class Zone:
with open(filepath, 'w') as file: with open(filepath, 'w') as file:
for record in self.records: for record in self.records:
file.write(str(record) + '\n') file.write(str(record) + '\n')
print(str(record))
file.close() file.close()

View file

@ -1,8 +1,9 @@
import main import main
zone = main.Zone() zone = main.Zone(origin='example.com')
soa = main.SOA() #soa = main.SOA()
record = main.Record(data='192.168.5.254', name='localhost.example.com') record = main.Record(data='192.168.5.254', name='localhost.example.com')
zone.add(soa) #zone.add(soa)
zone.new_soa(mname='ns1.')
zone.add(record) zone.add(record)
zone.save_file('/tmp/zone.txt') zone.save_file('/tmp/zone.txt')