source: trunk/pxeconfig/pxeconfig @ 13

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

pxeconfig has some changes that i did not knew!! and some documentation
errors again.

File size: 4.9 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/21 15:38:01 $
10#  $Revision: 1.6 $
11#
12# Copyright (C) 2000, SARA.
13#
14# Permission to use, copy, modify, and distribute this software and its
15# documentation for non-commercial purposes and without fee is hereby
16# granted, provided that the above copyright notice appears in all copies
17# and that both the copyright notice and this permission notice appear in
18# supporting documentation.
19#
20# Neither SARA (Stichting Academisch Rekencentrum Amsterdam) nor the
21# nor the author make any representations about the suitability of this
22# software for any purpose. This software is provided ``as is'' without
23# express or implied warranty.
24#
25#
26"""
27Usage: pxeconfig [-d|--directory <pxe_config_dir> ]
28
29With this program you can configure which PXE configuration file
30to use when a node boots. The program will ask the following questions:
31  1) Network address (Class C-network address only)
32  2) Starting number
33  3) Ending number
34  4) Which PXE config file to use
35
36For example, if the answers are:
37  1) 10.168.44
38  2) 2
39  3) 4
40  4) default.node_install
41
42Then the result is that is create symbolic links in /tftpboot/pxelinux.cfg:
43  0AA82C02 ----> default.node_install
44  0AA82C03 ----> default.node_install
45  0AA82C04 ----> default.node_install
46"""
47
48import string
49import sys
50import os
51import glob
52import getopt
53
54# DEBUG
55#
56DEBUG=0
57
58# Constants
59#
60PXE_CONF_DIR='/tftpboot/pxelinux.cfg'
61NADDR=0
62PXEFILE=1
63START=2
64END=3
65SHORTOPT_LIST='d:'
66LONGOPT_LIST=['directory=']
67
68def choice_pxe_configfile():
69  """
70  Let user choose which pxeconfig file to use.
71  """
72
73  os.chdir(PXE_CONF_DIR)
74
75  # Try to determine to which file the default symlink points, and
76  # if it exists, remove it from the list.
77  #
78  try:
79    default_file = os.readlink('default')
80  except OSError:
81    default_file = None
82    pass
83
84  files = glob.glob('default.*')
85  if not files:
86    print 'There are no pxe config files starting with: default.'
87    sys.exit(1)
88
89  if default_file:
90    files.remove(default_file)
91 
92
93  print 'Which pxe config file must we use: ?' 
94  i = 1   
95  for file in files:
96    print "%d : %s" %(i,file)
97    i = i +1
98
99  while 1:
100    index = raw_input('Choice a number: ')
101    try:
102      index = int(index)
103    except ValueError:
104      index = len(files) + 1
105
106    # Is the user smart enough to select
107    # the right value??
108    #
109    if 0 < index <= len(files): break
110
111  return files[index-1]
112
113def create_links(list):
114  """
115  Create the links in the PXE_CONF_DIR,
116    list : A list containing: network hex address, pxe config file,
117           start number, end number
118  """
119  os.chdir(PXE_CONF_DIR)
120  naddr = list[NADDR]
121  pxe_filename = list[PXEFILE]
122  for i in range(list[START], list[END]+1):
123    haddr = '%s%02X' %(naddr, i)
124    if DEBUG:
125      print 'linking %s to %s' %(haddr, pxe_filename)
126    os.symlink(pxe_filename, haddr)
127
128
129def check_network(net):
130  """
131  This function checks if the give network is a Class C-network and will
132  convert the network address to a hex address if true.
133  """
134  d = string.split(net, '.')
135
136  if len(d) != 3:
137    print('Only C-class nerwork addresses are supported!!!')
138    sys.exit(1)
139
140  # Check if we have valid network values
141  r = ''
142  for i in d:
143    i = check_number(i)
144    r = '%s%02X' %(r,i)
145
146  if DEBUG:
147    print r
148
149  return r
150
151def check_number(number):
152  """
153  This functions checks if the input is between 0 < number < 255:
154     number : is a string
155  """
156  try:
157    n = int(number)
158  except ValueError, detail:
159    print detail
160    sys.exit(1)
161
162  if 0 <= n <= 255:
163    return n
164  else:
165    print '%s is not a valid number, must be between 0 and 255' %number
166    sys.exit(1)
167
168def interactive():
169
170  iptable = {}
171
172  print __doc__
173
174  network = raw_input('Give network address (xxx.xxx.xxx): ') 
175  naddr = check_network(network)
176
177  start = raw_input('Starting number: ') 
178  start = check_number(start)
179
180  end = raw_input('Ending number: ') 
181  end = check_number(end)
182
183  pxe_filename = choice_pxe_configfile()
184
185  iptable[network] = []
186  iptable[network].append(naddr)
187  iptable[network].append(pxe_filename)
188  iptable[network].append(int(start))
189  iptable[network].append(int(end))
190
191  for net in iptable.keys():
192    if DEBUG:
193      print net, iptable[net]   
194    create_links(iptable[net])
195
196
197def check_args(argv):
198  """
199  command line option:
200    -d/--directory <dir>
201      Where <dir> is the directory where the pxe config files reside.
202  """
203  global PXE_CONF_DIR
204  try:
205    opts, args = getopt.getopt(argv[1:], SHORTOPT_LIST, LONGOPT_LIST)
206  except getopt.error, detail:
207    print check_args.__doc__
208    print detail
209    sys.exit(1)
210
211  if opts:
212    opt, PXE_CONF_DIR = opts[0]
213
214  if not os.path.isdir(PXE_CONF_DIR):
215    print 'Directory %s does not exists' %PXE_CONF_DIR
216    sys.exit(1)
217
218def main():
219  check_args(sys.argv)
220  interactive()
221
222if __name__ == '__main__':
223  main()
Note: See TracBrowser for help on using the repository browser.