4
from datetime import date
5
os.environ['DJANGO_SETTINGS_MODULE'] = 'Hostbase.settings'
6
from Hostbase.hostbase.models import *
7
from Hostbase.settings import DEFAULT_MX, PRIORITY
10
host_attribs = ['hostname', 'whatami', 'netgroup', 'security_class', 'support',
11
'csi', 'printq', 'dhcp', 'outbound_smtp', 'primary_user',
12
'administrator', 'location', 'expiration_date', 'comments']
14
def handle_error(field):
17
print "Error: %s is already defined in hostbase" % field
21
def checkformat(values, indices):
22
"""Ensures file contains all necessary attributes in order """
23
filelist = [pair[0] for pair in values]
25
# lines = len(filelist)
28
filelist = filelist[index:]
29
if filelist[0:14] != host_attribs:
30
# figure out what to do here
33
# process rest of host attributes
35
next = filelist[1:].index('hostname')
36
remaining = filelist[14:next+1]
38
remaining = filelist[14:]
39
needfields = ['mac_addr', 'hdwr_type', 'ip_addr']
40
if [item for item in needfields if item not in remaining]:
45
if __name__ == '__main__':
47
# argument handling for batchadd
49
fd = open(sys.argv[1], 'r')
50
except (IndexError, IOError):
51
print "\nUsage: batchadd.py filename\n"
54
lines = fd.readlines()
55
# splits and strips lines into (attribute, value)
56
info = [[item.strip() for item in line.split("->")] for line in lines
57
if line.lstrip(' ')[0] != '#' and line != '\n']
59
if info[0][0] == 'mx' and info[1][0] == 'priority':
60
mx, created = MX.objects.get_or_create(mx=info[0][1], priority=info[1][1])
64
mx, created = MX.objects.get_or_create(mx=DEFAULT_MX, priority=PRIORITY)
68
hostindices = [num for num in range(0, len(info)) if info[num][0] == 'hostname']
70
if not checkformat(info, hostindices):
71
print "Error: file format"
76
for host in hostindices:
78
host = Host.objects.get(hostname=info[host][1])
79
handle_error(info[host][1])
84
macindices = [num for num in range(0, len(info)) if info[num][0] == 'mac_addr']
85
for mac_addr in macindices:
87
host = Interface.objects.get(mac_addr=info[mac_addr][1])
88
handle_error(info[mac_addr][1])
93
for host in hostindices:
95
for attrib in host_attribs:
97
if pair[0] == 'dhcp' or pair[0] == 'outbound_smtp':
99
blank.__dict__[pair[0]] = 1
101
blank.__dict__[pair[0]] = 0
102
elif pair[0] == 'expiration_date':
103
(year, month, day) = pair[1].split("-")
104
blank.expiration_date = date(int(year), int(month), int(day))
106
blank.__dict__[pair[0]] = pair[1]
107
blank.status = 'active'
109
newhostname = blank.hostname.split(".")[0]
110
newdomain = blank.hostname.split(".", 1)[1]
111
while info and info[0][0] != 'hostname':
112
if info[0][0] == 'mac_addr':
114
inter = Interface.objects.create(host=blank, mac_addr=pair[1], hdwr_type='eth')
116
elif info[0][0] == 'hdwr_type':
118
inter.hdwr_type = pair[1]
120
elif info[0][0] == 'ip_addr':
122
ip = IP.objects.create(interface=inter, ip_addr=pair[1], num=1)
123
hostnamenode = Name(ip=ip, name=blank.hostname, dns_view='global', only=False)
125
namenode = Name(ip=ip, name=".".join([newhostname + "-" + inter.hdwr_type,
127
dns_view="global", only=False)
129
subnetnode = Name(ip=ip, name=newhostname + "-" +
130
ip.ip_addr.split(".")[2] + "." +
131
newdomain, dns_view="global", only=False)
133
hostnamenode.mxs.add(mx)
135
subnetnode.mxs.add(mx)
136
elif info[0][0] == 'cname':
138
cname = CName.objects.create(name=hostnamenode, cname=pair[1])