Changeset 186 for trunk/src


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

Location:
trunk/src
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/pxe_global.py.in

    r170 r186  
    4444
    4545        stanza = config.defaults()
    46         return stanza
     46        return config, stanza
  • trunk/src/pxeconfig.py

    r179 r186  
    174174        return r
    175175
    176 def hosts_2_hex(hosts, options):
     176def host_2_hex(host, options):
    177177        """
    178178        Convert hostname(s) to a net address that can be handled by manage_links function
     
    182182                print str
    183183
    184         for host in hosts:
    185                 try:
    186                         addr = socket.gethostbyname(host)
    187                 except socket.error,detail:
    188                         error =  '%s not an valid hostname: %s' %(host,detail)
    189                         raise PxeConfig, error
    190 
    191                 net = string.splitfields(addr, '.')
    192                 cnet = string.joinfields(net[0:3], '.')
    193 
    194                 if options.SCRIPT_HOOK:
    195                         if options.DEBUG or options.DRY_RUN or options.VERBOSE:
    196                                 print 'Executing client script hook: %s with arg: %s' %(options.SCRIPT_HOOK, addr)
    197                         if not options.DRY_RUN:
    198                                 cmd = '%s %s' %(options.SCRIPT_HOOK, addr)
    199                                 os.system(cmd)
    200 
    201                 haddr = '%s%02X' %(net_2_hex(cnet, options), int(net[3]))
    202                 manage_links(haddr, options)
     184        try:
     185                addr = socket.gethostbyname(host)
     186        except socket.error,detail:
     187                error =  '%s not an valid hostname: %s' %(host,detail)
     188                raise PxeConfig, error
     189
     190        net = string.splitfields(addr, '.')
     191        cnet = string.joinfields(net[0:3], '.')
     192
     193        if options.SCRIPT_HOOK:
     194                if options.DEBUG or options.DRY_RUN or options.VERBOSE:
     195                        print 'Executing client script hook: %s with arg: %s' %(options.SCRIPT_HOOK, addr)
     196                if not options.DRY_RUN:
     197                        cmd = '%s %s' %(options.SCRIPT_HOOK, addr)
     198                        os.system(cmd)
     199
     200        haddr = '%s%02X' %(net_2_hex(cnet, options), int(net[3]))
     201        manage_links(haddr, options)
     202
     203
     204def mac_2_hex(mac_addr, options):
     205        """
     206        Convert mac address to pxeconfig address
     207        """
     208        if options.DEBUG:
     209                str = 'mac_2_hex: %s' %mac_addr
     210                print mac_addr
     211
     212        haddr = '01-%s' %(mac_addr.replace(':', '-').lower())
     213        manage_links(haddr, options)
    203214
    204215def add_options(p):
     
    251262        )
    252263
    253 def parser(argv, settings):
     264def parser(argv, config, defaults):
    254265        """
    255266        Make use of sara advance parser module. It can handle regex in hostnames
     
    272283        if not options.DEBUG:
    273284                try:
    274                         if settings['debug']:
    275                                 options.DEBUG = int(settings['debug'])
     285                        if defaults['debug']:
     286                                options.DEBUG = int(defaults['debug'])
    276287                except KeyError:
    277288                        pass
     
    290301        # ...
    291302        try:
    292                 options.SCRIPT_HOOK = settings['client_script_hook']
     303                options.SCRIPT_HOOK = defaults['client_script_hook']
    293304        except KeyError, detail:
    294305                pass
     
    297308                print args, options
    298309
    299         hosts_2_hex(args, options)
    300 
     310        ##
     311        # Are the hosts wiht only mac addresses defined in the configuration file
     312
     313        for host in args:
     314                if host in config.sections():
     315                        mac_addr = config.get(host, 'mac_address')
     316                        mac_2_hex(mac_addr, options)
     317                else:
     318                        host_2_hex(host, options)
    301319
    302320def main():
     
    304322        #
    305323        global PXE_CONF_DIR
    306         settings = ReadConfig()
    307        
    308         try:
    309                 PXE_CONF_DIR = settings['pxe_config_dir']
     324        parser_config, default_settings = ReadConfig()
     325       
     326        try:
     327                PXE_CONF_DIR = default_settings['pxe_config_dir']
    310328
    311329        except KeyError:
     
    317335                raise PxeConfig, error
    318336
    319         parser(sys.argv, settings)
     337        parser(sys.argv, parser_config, default_settings)
    320338
    321339       
  • 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.