source: trunk/pxeconfig/pxeconfigd @ 27

Last change on this file since 27 was 27, checked in by sscpbas, 21 years ago

pxeconfigd:

Moved CVS info to the comment lines instead of python documentation.

File size: 3.8 KB
RevLine 
[2]1#!/usr/bin/env python
[6]2#
[14]3# Copyright (C) 2002
[6]4#
[14]5# This file is part of the pxeconfig utils
[6]6#
[14]7# This program is free software; you can redistribute it and/or modify it
8# under the terms of the GNU General Public License as published by the
9# Free Software Foundation; either version 2, or (at your option) any
10# later version.
[6]11#
[14]12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program; if not, write to the Free Software
19# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
20#
[27]21# CVS info
22#  $Date: 2002/11/29 16:31:15 $
23#  $Revision: 1.8 $
[2]24"""
25Author: Bas van der Vlies <basv@sara.nl>
26Date  : 12 February 2002
27Desc. : This script is used to control how a node is booted with PXE
[3]28        enabled network cards. The node boots and fetch a pxe
29        config file which tells how the node must boot. This daemon
30        enables a client to remove his/here pxe config file. With the
31        next boot it will use the default one.
32
33        command line option:
34          -d/--directory <dir>
35            Where <dir> is the directory where the pxe config files reside.
[26]36          -V/--version
37            Prints the version number and exits
[2]38         
39        Note:
40          This server can ONLY be started from inetd.
41"""
42import time
43import socket
44import sys
45import os
46import string
47import syslog
[3]48import getopt
[2]49
50# DEBUG
51#
52DEBUG=1
53
54# Some Global Constants
55#
56BUFSIZE=4096
57STDIN=0
58STDOUT=1
[25]59SHORTOPT_LIST='Vd:'
60LONGOPT_LIST=['version', 'directory=']
[2]61
62PXE_CONF_DIR = '/tftpboot/pxelinux.cfg'
[25]63VERSION = '0.4.1'
[2]64
65# Give the current time
66#
67def now():
68    return time.ctime(time.time())
69
70
71def remove_link(filename):
72  """This removes the pxe config filename for the host that is connected:
73      filename : string
74  """
75
76  file = os.path.join(PXE_CONF_DIR, filename)
77
78  if DEBUG:
79    print 'file = %s' %file
80
81  if not os.path.exists(file): return
82
83  if os.path.islink(file): 
84    try:
85      os.unlink(file)
86      syslog.openlog("pxeconfigd")
87      syslog.syslog(syslog.LOG_INFO, file)
88      syslog.closelog()
89    except OSError:
90      err_msg = "No permission at directory: %s" %PXE_CONF_DIR
91      os.write(STDOUT, err_msg)
92      sys.exit(1)
93
94# This function handles the client connection. It closes
95# the connection if there is no data
96#
97def handleConnection():
98  """Determines which host connects to the server
99  """
100
101  # Determine client address
102  #
103  try:
104    client_addr = socket.fromfd(sys.stdin.fileno(), socket.AF_INET, 
105                          socket.SOCK_STREAM)
106    client_ip = client_addr.getpeername()[0] 
107
108  except socket.error, detail:
109    print "pxeconfigd can only be started from inetd!!!"
110    sys.exit(1)
111
112
113  # translate ip address ---> hex address
114  #
115  d = string.split(client_ip, '.')
116  client_haddr = '%02X%02X%02X%02X' %(int(d[0]), int(d[1]), int(d[2]), int(d[3]))
117
118  if DEBUG:
119    print 'ip = %s, hex = %s' %(client_ip, client_haddr)
120
121  remove_link(client_haddr)
122  sys.exit(0)
123
[3]124def check_args(argv):
125  """
126  Must we use another directory for the PXE configuration
127  """
[4]128  global PXE_CONF_DIR
129
[3]130  try:
131    opts, args = getopt.getopt(argv[1:], SHORTOPT_LIST, LONGOPT_LIST)
132  except getopt.error, detail:
133    print __doc__
134    print detail
135    sys.exit(1)
[2]136
[25]137  for opt, value in opts:
[3]138
[25]139    if opt in ['-d', '--directory']:
140      if not os.path.isdir(value):
141        os.write(STDOUT, "PXE booting is not configured")
142        sys.exit(1)
143      else:
144        PXE_CONF_DIR = value
145
146    elif opt in ['-V', '--version']:
147      print VERSION
148      sys.exit(0)
149
[2]150def server():
151  """Start the daemon
152  """
[3]153  check_args(sys.argv)
[2]154  handleConnection()
155
156if __name__ == '__main__':
157  server()
Note: See TracBrowser for help on using the repository browser.