#!/usr/bin/env python # # Author: Bas van der Vlies # Date : 16 February 2002 # # Tester: Walter de Jong # # CVS info # $Date: 2002/08/16 07:43:21 $ # $Revision: 1.9 $ # # Copyright (C) 2002 # # This file is part of the pxeconfig utils # # This program is free software; you can redistribute it and/or modify it # under the terms of the GNU General Public License as published by the # Free Software Foundation; either version 2, or (at your option) any # later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA # """ Usage: pxeconfig [-d|--directory ] With this program you can configure which PXE configuration file to use when a node boots. The program will ask the following questions: 1) Network address (Class C-network address only) 2) Starting number 3) Ending number 4) Which PXE config file to use For example, if the answers are: 1) 10.168.44 2) 2 3) 4 4) default.node_install Then the result is that is create symbolic links in /tftpboot/pxelinux.cfg: 0AA82C02 ----> default.node_install 0AA82C03 ----> default.node_install 0AA82C04 ----> default.node_install """ import string import sys import os import glob import getopt # DEBUG # DEBUG=0 # Constants # PXE_CONF_DIR='/tftpboot/pxelinux.cfg' NADDR=0 PXEFILE=1 START=2 END=3 SHORTOPT_LIST='d:' LONGOPT_LIST=['directory='] def choice_pxe_configfile(): """ Let user choose which pxeconfig file to use. """ os.chdir(PXE_CONF_DIR) # Try to determine to which file the default symlink points, and # if it exists, remove it from the list. # try: default_file = os.readlink('default') except OSError: default_file = None pass files = glob.glob('default.*') if not files: print 'There are no pxe config files starting with: default.' sys.exit(1) if default_file: files.remove(default_file) print 'Which pxe config file must we use: ?' i = 1 for file in files: print "%d : %s" %(i,file) i = i +1 while 1: index = raw_input('Select a number: ') try: index = int(index) except ValueError: index = len(files) + 1 # Is the user smart enough to select # the right value?? # if 0 < index <= len(files): break return files[index-1] def create_links(list): """ Create the links in the PXE_CONF_DIR, list : A list containing: network hex address, pxe config file, start number, end number """ os.chdir(PXE_CONF_DIR) naddr = list[NADDR] pxe_filename = list[PXEFILE] for i in range(list[START], list[END]+1): haddr = '%s%02X' %(naddr, i) if DEBUG: print 'linking %s to %s' %(haddr, pxe_filename) if os.path.exists(haddr): os.unlink(haddr) os.symlink(pxe_filename, haddr) def check_network(net): """ This function checks if the give network is a Class C-network and will convert the network address to a hex address if true. """ d = string.split(net, '.') if len(d) != 3: print('Only C-class nerwork addresses are supported!!!') sys.exit(1) # Check if we have valid network values r = '' for i in d: i = check_number(i) r = '%s%02X' %(r,i) if DEBUG: print r return r def check_number(number): """ This functions checks if the input is between 0 < number < 255: number : is a string """ try: n = int(number) except ValueError, detail: print detail sys.exit(1) if 0 <= n <= 255: return n else: print '%s is not a valid number, must be between 0 and 255' %number sys.exit(1) def interactive(): iptable = {} print __doc__ network = raw_input('Give network address (xxx.xxx.xxx): ') naddr = check_network(network) start = raw_input('Starting number: ') start = check_number(start) end = raw_input('Ending number: ') end = check_number(end) pxe_filename = choice_pxe_configfile() iptable[network] = [] iptable[network].append(naddr) iptable[network].append(pxe_filename) iptable[network].append(int(start)) iptable[network].append(int(end)) for net in iptable.keys(): if DEBUG: print net, iptable[net] create_links(iptable[net]) def check_args(argv): """ command line option: -d/--directory Where is the directory where the pxe config files reside. """ global PXE_CONF_DIR try: opts, args = getopt.getopt(argv[1:], SHORTOPT_LIST, LONGOPT_LIST) except getopt.error, detail: print check_args.__doc__ print detail sys.exit(1) if opts: opt, PXE_CONF_DIR = opts[0] if not os.path.isdir(PXE_CONF_DIR): print 'Directory %s does not exists' %PXE_CONF_DIR sys.exit(1) def main(): check_args(sys.argv) interactive() if __name__ == '__main__': main()