source: trunk/pxeconfig/pxeconfigd @ 11

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

Added the license info to each utility and fix and error in the getopt
handling for pxeconfig utility

File size: 3.4 KB
Line 
1#!/usr/bin/env python
2# Copyright (C) 2000, SARA.
3#
4# Permission to use, copy, modify, and distribute this software and its
5# documentation for non-commercial purposes and without fee is hereby
6# granted, provided that the above copyright notice appears in all copies
7# and that both the copyright notice and this permission notice appear in
8# supporting documentation.
9#
10# Neither SARA (Stichting Academisch Rekencentrum Amsterdam) nor the
11# nor the author make any representations about the suitability of this
12# software for any purpose. This software is provided ``as is'' without
13# express or implied warranty.
14#
15#
16"""
17Author: Bas van der Vlies <basv@sara.nl>
18Date  : 12 February 2002
19Desc. : This script is used to control how a node is booted with PXE
20        enabled network cards. The node boots and fetch a pxe
21        config file which tells how the node must boot. This daemon
22        enables a client to remove his/here pxe config file. With the
23        next boot it will use the default one.
24
25        command line option:
26          -d/--directory <dir>
27            Where <dir> is the directory where the pxe config files reside.
28         
29        Note:
30          This server can ONLY be started from inetd.
31
32CVS info
33  $Date: 2002/02/21 13:21:52 $
34  $Revision: 1.4 $
35"""
36import time
37import socket
38import sys
39import os
40import string
41import syslog
42import getopt
43
44# DEBUG
45#
46DEBUG=1
47
48# Some Global Constants
49#
50BUFSIZE=4096
51STDIN=0
52STDOUT=1
53SHORTOPT_LIST='d:'
54LONGOPT_LIST=['directory=']
55
56PXE_CONF_DIR = '/tftpboot/pxelinux.cfg'
57
58# Give the current time
59#
60def now():
61    return time.ctime(time.time())
62
63
64def remove_link(filename):
65  """This removes the pxe config filename for the host that is connected:
66      filename : string
67  """
68
69  file = os.path.join(PXE_CONF_DIR, filename)
70
71  if DEBUG:
72    print 'file = %s' %file
73
74  if not os.path.exists(file): return
75
76  if os.path.islink(file): 
77    try:
78      os.unlink(file)
79      syslog.openlog("pxeconfigd")
80      syslog.syslog(syslog.LOG_INFO, file)
81      syslog.closelog()
82    except OSError:
83      err_msg = "No permission at directory: %s" %PXE_CONF_DIR
84      os.write(STDOUT, err_msg)
85      sys.exit(1)
86
87# This function handles the client connection. It closes
88# the connection if there is no data
89#
90def handleConnection():
91  """Determines which host connects to the server
92  """
93
94  # Determine client address
95  #
96  try:
97    client_addr = socket.fromfd(sys.stdin.fileno(), socket.AF_INET, 
98                          socket.SOCK_STREAM)
99    client_ip = client_addr.getpeername()[0] 
100
101  except socket.error, detail:
102    print "pxeconfigd can only be started from inetd!!!"
103    sys.exit(1)
104
105
106  # translate ip address ---> hex address
107  #
108  d = string.split(client_ip, '.')
109  client_haddr = '%02X%02X%02X%02X' %(int(d[0]), int(d[1]), int(d[2]), int(d[3]))
110
111  if DEBUG:
112    print 'ip = %s, hex = %s' %(client_ip, client_haddr)
113
114  remove_link(client_haddr)
115  sys.exit(0)
116
117def check_args(argv):
118  """
119  Must we use another directory for the PXE configuration
120  """
121  global PXE_CONF_DIR
122
123  try:
124    opts, args = getopt.getopt(argv[1:], SHORTOPT_LIST, LONGOPT_LIST)
125  except getopt.error, detail:
126    print __doc__
127    print detail
128    sys.exit(1)
129
130  if opts:
131    opt, PXE_CONF_DIR = opts[0]
132
133def server():
134  """Start the daemon
135  """
136  check_args(sys.argv)
137
138  if not os.path.isdir(PXE_CONF_DIR):
139    os.write(STDOUT, "PXE booting is not configured")
140    sys.exit(1)
141
142  handleConnection()
143
144if __name__ == '__main__':
145  server()
Note: See TracBrowser for help on using the repository browser.