source: trunk/pxeconfig/pxeconfig @ 2

Last change on this file since 2 was 2, checked in by sscpbas, 22 years ago

Initial revision

File size: 3.6 KB
Line 
1#!/usr/bin/env python
2#
3# Author: Bas van der Vlies <basv@sara.nl>
4# Date  : 16 February 2002
5#
6# Tester: Walter de Jong <walter@sara.nl>
7#
8# CVS info
9#  $Date: 2002/02/18 15:02:00 $
10#  $Revision: 1.1 $
11#
12"""
13With this program you can configure which PXE configuration file
14to use when a node boots. The programm will ask the following questions:
15  1) Network address (Class C-network address only)
16  2) Starting nummber
17  3) Ending number
18  4) Which PXE config file to use.
19
20For example, if the answers are:
21  1) 10.168.44
22  2) 2
23  3) 4
24  4) default.node_install
25
26Then the result is that is create symbolic links in /tftpboot/pxelinux.cfg:
27  0AA82C02 ----> default.node_install
28  0AA82C03 ----> default.node_install
29  0AA82C04 ----> default.node_install
30"""
31
32import string
33import sys
34import os
35import glob
36
37# DEBUG
38#
39DEBUG=0
40
41# Constants
42#
43PXE_CONFIG_DIR='/tftpboot/pxelinux.cfg'
44NADDR=0
45PXEFILE=1
46START=2
47END=3
48
49def choice_pxe_configfile():
50  """
51  Let user choice which pxeconfig file to use.
52  """
53
54  os.chdir(PXE_CONFIG_DIR)
55
56  # Try to determine to which default file point to and
57  # if exists remove it from te list.
58  #
59  try:
60    default_file = os.readlink('default')
61  except OSError:
62    default_file = None
63    pass
64
65  files = glob.glob('default.*')
66  if not files:
67    print 'There are no pxe config files starting with: default.'
68    sys.exit(1)
69
70  if default_file:
71    files.remove(default_file)
72 
73
74  print 'Which pxe config file must we use: ?' 
75  i = 1   
76  for file in files:
77    print "%d : %s" %(i,file)
78    i = i +1
79
80  while 1:
81    index = raw_input('Choice a number: ')
82    try:
83      index = int(index)
84    except ValueError:
85      index = len(files) + 1
86
87    # Is the user smart enough to select
88    # the right value
89    #
90    if 0 < index <= len(files): break
91
92  return files[index-1]
93
94def create_links(list):
95  """
96  Create the links in the PXE_CONFIG_DIR,
97    list : A list containing: network hex addres, pxe config file,
98           start number, end number
99  """
100  os.chdir(PXE_CONFIG_DIR)
101  naddr = list[NADDR]
102  pxe_filename = list[PXEFILE]
103  for i in range(list[START], list[END]+1):
104    haddr = '%s%02X' %(naddr, i)
105    if DEBUG:
106      print 'linking %s to %s' %(haddr, pxe_filename)
107    os.symlink(pxe_filename, haddr)
108
109
110def check_network(net):
111  """
112  This function check if the give network is a Class C-network and will
113  convert the network address to a hex address if true.
114  """
115  d = string.split(net, '.')
116
117  if len(d) != 3:
118    print('Only C-class nerwork addresses are supported!!!')
119    sys.exit(1)
120
121  # Check if we have valid network values
122  r = ''
123  for i in d:
124    i = check_number(i)
125    r = '%s%02X' %(r,i)
126
127  if DEBUG:
128    print r
129
130  return r
131
132def check_number(number):
133  """
134  This functions checks if the input is between 0 < number < 255:
135     number : is a string
136  """
137  try:
138    n = int(number)
139  except ValueError, detail:
140    print detail
141    sys.exit(1)
142
143  if 0 <= n <= 255:
144    return n
145  else:
146    print '%s is not a valid number, must be between 0 and 255' %number
147    sys.exit(1)
148
149def interactive():
150
151  iptable = {}
152
153  print __doc__
154
155  network = raw_input('Give network addres (xxx.xxx.xxx): ') 
156  naddr = check_network(network)
157
158  start = raw_input('Starting number: ') 
159  start = check_number(start)
160
161  end = raw_input('Ending number: ') 
162  end = check_number(end)
163
164  pxe_filename = choice_pxe_configfile()
165
166  iptable[network] = []
167  iptable[network].append(naddr)
168  iptable[network].append(pxe_filename)
169  iptable[network].append(int(start))
170  iptable[network].append(int(end))
171
172
173  for net in iptable.keys():
174    if DEBUG:
175      print net, iptable[net]   
176    create_links(iptable[net])
177
178def main():
179  interactive()
180
181if __name__ == '__main__':
182  main()
Note: See TracBrowser for help on using the repository browser.