source: trunk/pxeconfig/pxeconfigd @ 3

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

Added command line option to specifiy where the pxe config directory resides.
This is for client ans server side.

Added some info about the package such as a READMEE, AUTHORS and LICENSE.SARA
file.

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:38:10 $
20  $Revision: 1.2 $
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  try:
108    opts, args = getopt.getopt(argv[1:], SHORTOPT_LIST, LONGOPT_LIST)
109  except getopt.error, detail:
110    print __doc__
111    print detail
112    sys.exit(1)
113
114  if opts:
115    opt, PXE_CONFIG_DIR = opts[0]
116
117 
118 
119
120
121def server():
122  """Start the daemon
123  """
124  check_args(sys.argv)
125
126  if not os.path.isdir(PXE_CONF_DIR):
127    os.write(STDOUT, "PXE booting is not configured")
128    sys.exit(1)
129
130  handleConnection()
131
132if __name__ == '__main__':
133  server()
Note: See TracBrowser for help on using the repository browser.