source: trunk/pxeconfig/pxeconfigd @ 4

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

Fixed an error in the getopt handling for the client and server.

Addes some pxe config files

File size: 2.8 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 node boots and fetch a pxe
7        config file which tells how the node must boot. This daemon
8        enables a client to remove his/here pxe config file. With the
9        next boot it will use the default one.
10
11        command line option:
12          -d/--directory <dir>
13            Where <dir> is the directory where the pxe config files reside.
14         
15        Note:
16          This server can ONLY be started from inetd.
17
18CVS info
19  $Date: 2002/02/19 09:57:40 $
20  $Revision: 1.3 $
21"""
22import time
23import socket
24import sys
25import os
26import string
27import syslog
28import getopt
29
30# DEBUG
31#
32DEBUG=1
33
34# Some Global Constants
35#
36BUFSIZE=4096
37STDIN=0
38STDOUT=1
39SHORTOPT_LIST='d:'
40LONGOPT_LIST=['directory=']
41
42PXE_CONF_DIR = '/tftpboot/pxelinux.cfg'
43
44# Give the current time
45#
46def now():
47    return time.ctime(time.time())
48
49
50def remove_link(filename):
51  """This removes the pxe config filename for the host that is connected:
52      filename : string
53  """
54
55  file = os.path.join(PXE_CONF_DIR, filename)
56
57  if DEBUG:
58    print 'file = %s' %file
59
60  if not os.path.exists(file): return
61
62  if os.path.islink(file): 
63    try:
64      os.unlink(file)
65      syslog.openlog("pxeconfigd")
66      syslog.syslog(syslog.LOG_INFO, file)
67      syslog.closelog()
68    except OSError:
69      err_msg = "No permission at directory: %s" %PXE_CONF_DIR
70      os.write(STDOUT, err_msg)
71      sys.exit(1)
72
73# This function handles the client connection. It closes
74# the connection if there is no data
75#
76def handleConnection():
77  """Determines which host connects to the server
78  """
79
80  # Determine client address
81  #
82  try:
83    client_addr = socket.fromfd(sys.stdin.fileno(), socket.AF_INET, 
84                          socket.SOCK_STREAM)
85    client_ip = client_addr.getpeername()[0] 
86
87  except socket.error, detail:
88    print "pxeconfigd can only be started from inetd!!!"
89    sys.exit(1)
90
91
92  # translate ip address ---> hex address
93  #
94  d = string.split(client_ip, '.')
95  client_haddr = '%02X%02X%02X%02X' %(int(d[0]), int(d[1]), int(d[2]), int(d[3]))
96
97  if DEBUG:
98    print 'ip = %s, hex = %s' %(client_ip, client_haddr)
99
100  remove_link(client_haddr)
101  sys.exit(0)
102
103def check_args(argv):
104  """
105  Must we use another directory for the PXE configuration
106  """
107  global PXE_CONF_DIR
108
109  try:
110    opts, args = getopt.getopt(argv[1:], SHORTOPT_LIST, LONGOPT_LIST)
111  except getopt.error, detail:
112    print __doc__
113    print detail
114    sys.exit(1)
115
116  if opts:
117    opt, PXE_CONF_DIR = opts[0]
118
119def server():
120  """Start the daemon
121  """
122  check_args(sys.argv)
123
124  if not os.path.isdir(PXE_CONF_DIR):
125    os.write(STDOUT, "PXE booting is not configured")
126    sys.exit(1)
127
128  handleConnection()
129
130if __name__ == '__main__':
131  server()
Note: See TracBrowser for help on using the repository browser.