source: trunk/pxeconfig/pxeconfig @ 3

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

Added command line option to specifiy where the pxe config directory resides.
This is for client ans server side.

Added some info about the package such as a READMEE, AUTHORS and LICENSE.SARA
file.

File size: 4.0 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/19 09:38:10 $
10#  $Revision: 1.2 $
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
36import getopt
37
38# DEBUG
39#
40DEBUG=0
41
42# Constants
43#
44PXE_CONFIG_DIR='/tftpboot/pxelinux.cfg'
45NADDR=0
46PXEFILE=1
47START=2
48END=3
49SHORTOPT_LIST='d:'
50LONGOPT_LIST=['directory=']
51
52def choice_pxe_configfile():
53  """
54  Let user choice which pxeconfig file to use.
55  """
56
57  os.chdir(PXE_CONFIG_DIR)
58
59  # Try to determine to which default file point to and
60  # if exists remove it from te list.
61  #
62  try:
63    default_file = os.readlink('default')
64  except OSError:
65    default_file = None
66    pass
67
68  files = glob.glob('default.*')
69  if not files:
70    print 'There are no pxe config files starting with: default.'
71    sys.exit(1)
72
73  if default_file:
74    files.remove(default_file)
75 
76
77  print 'Which pxe config file must we use: ?' 
78  i = 1   
79  for file in files:
80    print "%d : %s" %(i,file)
81    i = i +1
82
83  while 1:
84    index = raw_input('Choice a number: ')
85    try:
86      index = int(index)
87    except ValueError:
88      index = len(files) + 1
89
90    # Is the user smart enough to select
91    # the right value
92    #
93    if 0 < index <= len(files): break
94
95  return files[index-1]
96
97def create_links(list):
98  """
99  Create the links in the PXE_CONFIG_DIR,
100    list : A list containing: network hex addres, pxe config file,
101           start number, end number
102  """
103  os.chdir(PXE_CONFIG_DIR)
104  naddr = list[NADDR]
105  pxe_filename = list[PXEFILE]
106  for i in range(list[START], list[END]+1):
107    haddr = '%s%02X' %(naddr, i)
108    if DEBUG:
109      print 'linking %s to %s' %(haddr, pxe_filename)
110    os.symlink(pxe_filename, haddr)
111
112
113def check_network(net):
114  """
115  This function check if the give network is a Class C-network and will
116  convert the network address to a hex address if true.
117  """
118  d = string.split(net, '.')
119
120  if len(d) != 3:
121    print('Only C-class nerwork addresses are supported!!!')
122    sys.exit(1)
123
124  # Check if we have valid network values
125  r = ''
126  for i in d:
127    i = check_number(i)
128    r = '%s%02X' %(r,i)
129
130  if DEBUG:
131    print r
132
133  return r
134
135def check_number(number):
136  """
137  This functions checks if the input is between 0 < number < 255:
138     number : is a string
139  """
140  try:
141    n = int(number)
142  except ValueError, detail:
143    print detail
144    sys.exit(1)
145
146  if 0 <= n <= 255:
147    return n
148  else:
149    print '%s is not a valid number, must be between 0 and 255' %number
150    sys.exit(1)
151
152def interactive():
153
154  iptable = {}
155
156  print __doc__
157
158  network = raw_input('Give network addres (xxx.xxx.xxx): ') 
159  naddr = check_network(network)
160
161  start = raw_input('Starting number: ') 
162  start = check_number(start)
163
164  end = raw_input('Ending number: ') 
165  end = check_number(end)
166
167  pxe_filename = choice_pxe_configfile()
168
169  iptable[network] = []
170  iptable[network].append(naddr)
171  iptable[network].append(pxe_filename)
172  iptable[network].append(int(start))
173  iptable[network].append(int(end))
174
175
176  for net in iptable.keys():
177    if DEBUG:
178      print net, iptable[net]   
179    create_links(iptable[net])
180
181def check_args(argv):
182  """
183  Must we use another directory for the PXE configuration
184  """
185  try:
186    opts, args = getopt.getopt(argv[1:], SHORTOPT_LIST, LONGOPT_LIST)
187  except getopt.error, detail:
188    print __doc__
189    print detail
190    sys.exit(1)
191
192  if opts:
193    opt, PXE_CONFIG_DIR = opts[0]
194
195def main():
196  check_args(sys.argv)
197  interactive()
198
199if __name__ == '__main__':
200  main()
Note: See TracBrowser for help on using the repository browser.