]>
code.delx.au - monosys/blob - scripts/dyndns
3 from datetime
import datetime
10 def increment_serial(line
):
11 current_serial
= re
.search(R
"\b\d\d*\b", line
).group(0)
13 current
= int(current_serial
)
14 current_num
= current
% 100
15 current_date
= (current
- current_num
) / 100
16 new_date
= int(datetime
.now().strftime("%Y%m%d"))
17 if current_date
== new_date
:
18 next_num
= current_num
+ 1
23 raise ValueError("Too many serial changes today!")
24 new_serial
= str(new_date
) + str(next_num
).zfill(2)
25 line
= line
.replace(current_serial
, new_serial
)
30 source_ip
, source_port
, dest_ip
, dest_port
= os
.environ
["SSH_CONNECTION"].split()
31 line
= re
.sub(R
"\b\d\d?\d?\.\d\d?\d?\.\d\d?\d?\.\d\d?\d?\b", source_ip
, line
)
35 def main(live
, zonefile
, dnslabel
):
39 if line
.find("Serial") >= 0:
40 out
.append(increment_serial(line
))
41 elif line
.find("DYNDNS") >= 0 and line
.find("bnet") >= 0:
42 out
.append(replace_ip(line
))
49 outf
= open(zonefile
, "w")
52 subprocess
.check_call(["sudo", "/etc/init.d/bind9", "reload"])
54 sys
.stdout
.writelines(out
)
57 if __name__
== "__main__":
61 if sys
.argv
[1] == "live":
64 zonefile
= sys
.argv
[i
]
65 dnslabel
= sys
.argv
[i
+1]
67 print >>sys
.stderr
, "Usage: %s [go] zonefile dnslabel" % sys
.argv
[0]
70 main(live
, zonefile
, dnslabel
)