source: trunk/src/pxeconfigd.py @ 170

Last change on this file since 170 was 169, checked in by bas, 15 years ago

renamed src/pxeconfigd.in to src/pxeconfigd.py to use it as module

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