source: trunk/pxeconfig/pxeconfigd @ 2

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

Initial revision

File size: 2.3 KB
Line 
1#!/usr/bin/env python
2"""
3Author: Bas van der Vlies <basv@sara.nl>
4Date  : 12 February 2002
5Desc. : This script is used to control how a node is booted with PXE
6        enabled network cards. The PC boots starts and fetch a pxe
7        config file which tells how the PC must boot. This daemon
8        enables a client to remove his pxe-config file. It then uses
9        the DEFAULT one.
10         
11        Note:
12          This server can ONLY be started from inetd.
13
14CVS info
15  $Date: 2002/02/18 15:02:00 $
16  $Revision: 1.1 $
17"""
18import time
19import socket
20import sys
21import os
22import string
23import syslog
24
25# DEBUG
26#
27DEBUG=1
28
29# Some Global Constants
30#
31BUFSIZE=4096
32STDIN=0
33STDOUT=1
34
35PXE_CONF_DIR = '/tftpboot/pxelinux.cfg'
36
37# Give the current time
38#
39def now():
40    return time.ctime(time.time())
41
42
43def remove_link(filename):
44  """This removes the pxe config filename for the host that is connected:
45      filename : string
46  """
47
48  file = os.path.join(PXE_CONF_DIR, filename)
49
50  if DEBUG:
51    print 'file = %s' %file
52
53  if not os.path.exists(file): return
54
55  if os.path.islink(file): 
56    try:
57      os.unlink(file)
58      syslog.openlog("pxeconfigd")
59      syslog.syslog(syslog.LOG_INFO, file)
60      syslog.closelog()
61    except OSError:
62      err_msg = "No permission at directory: %s" %PXE_CONF_DIR
63      os.write(STDOUT, err_msg)
64      sys.exit(1)
65
66# This function handles the client connection. It closes
67# the connection if there is no data
68#
69def handleConnection():
70  """Determines which host connects to the server
71  """
72
73  # Determine client address
74  #
75  try:
76    client_addr = socket.fromfd(sys.stdin.fileno(), socket.AF_INET, 
77                          socket.SOCK_STREAM)
78    client_ip = client_addr.getpeername()[0] 
79
80  except socket.error, detail:
81    print "pxeconfigd can only be started from inetd!!!"
82    sys.exit(1)
83
84
85  # translate ip address ---> hex address
86  #
87  d = string.split(client_ip, '.')
88  client_haddr = '%02X%02X%02X%02X' %(int(d[0]), int(d[1]), int(d[2]), int(d[3]))
89
90  if DEBUG:
91    print 'ip = %s, hex = %s' %(client_ip, client_haddr)
92
93  remove_link(client_haddr)
94  sys.exit(0)
95
96
97def server():
98  """Start the daemon
99  """
100
101  if not os.path.isdir(PXE_CONF_DIR):
102    os.write(STDOUT, "PXE booting is not configured")
103    sys.exit(1)
104
105  handleConnection()
106
107if __name__ == '__main__':
108  server()
Note: See TracBrowser for help on using the repository browser.