Ignore:
Timestamp:
10/28/10 16:04:25 (14 years ago)
Author:
bas
Message:

Added mac address support for environments that have dynamic ip's. Depended on arp command

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/pxeconfigd.py

    r177 r186  
    6767
    6868
    69 def remove_link(filename):
    70   """This removes the pxe config filename for the host that is connected:
    71       filename : string
    72   """
     69def remove_link(ip, arp_cmd):
     70        """
     71        This removes the pxe config filename for the host that is connected:
     72                ip      : ip address of the client host
     73                arp_cmd : For Dynamic ips, look up the mac address via arp
     74        """
    7375
    74   file = os.path.join(PXE_CONF_DIR, filename)
     76        ## translate ip address ---> hex address
     77        #
     78        d = string.split(ip, '.')
     79        haddr = '%02X%02X%02X%02X' %(int(d[0]), int(d[1]), int(d[2]), int(d[3]))
    7580
    76   if DEBUG:
    77     print 'file = %s' %file
     81        file = os.path.join(PXE_CONF_DIR, haddr)
    7882
    79   if not os.path.exists(file): return
     83        if not os.path.exists(file):
    8084
    81   if os.path.islink(file):
    82     try:
    83       os.unlink(file)
    84       syslog.openlog("pxeconfigd")
    85       syslog.syslog(syslog.LOG_INFO, file)
    86       syslog.closelog()
    87     except OSError:
    88       err_msg = "No permission at directory: %s" %PXE_CONF_DIR
    89       os.write(STDOUT, err_msg)
    90       sys.exit(1)
     85                ##
     86                # No ARP command set, just return
     87                #
     88                if not arp_cmd:
     89                        return
     90
     91                else:
     92                        ##
     93                        #  arp -n 192.168.146.112
     94                        # Address          HWtype  HWaddress           Flags Mask Iface
     95                        # 192.168.146.112  ether   00:23:ae:fd:cf:74   C          vlan133
     96
     97                        cmd = '%s %s' %(arp_cmd, ip)
     98                        lines = os.popen(cmd).readlines()
     99                        for line in lines:
     100                                if line.startswith(ip):
     101                                        t = line.split()
     102                                        mac_addr = t[2]
     103                                        haddr = '01-%s' %(mac_addr.replace(':', '-').lower())
     104                                        file = os.path.join(PXE_CONF_DIR, haddr)
     105                                        if not os.path.exists(file):
     106                                                return
     107
     108        if DEBUG:
     109                print 'file = %s' %file
     110
     111        if os.path.islink(file):
     112                try:
     113                        os.unlink(file)
     114                        syslog.openlog("pxeconfigd")
     115                        syslog.syslog(syslog.LOG_INFO, file)
     116                        syslog.closelog()
     117                except OSError:
     118                        err_msg = "No permission at directory: %s" %PXE_CONF_DIR
     119                        os.write(STDOUT, err_msg)
     120                        sys.exit(1)
     121
    91122
    92123# This function handles the client connection. It closes
     
    108139                raise PxeConfig, error
    109140               
    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]))
    114141       
    115142        if DEBUG:
     
    123150                pass
    124151
    125         remove_link(client_haddr)
     152        try:
     153                arp_cmd = settings['arp_command']
     154        except KeyError:
     155                arp_cmd = None
     156
     157        remove_link(client_ip, arp_cmd)
    126158        sys.exit(0)
    127159
     
    157189  """Start the daemon
    158190  """
    159   config = ReadConfig()
    160   check_args(sys.argv, config)
    161   handleConnection(config)
     191  parser_config, default_settings = ReadConfig()
     192  check_args(sys.argv, default_settings)
     193  handleConnection(default_settings)
    162194
    163195if __name__ == '__main__':
Note: See TracChangeset for help on using the changeset viewer.