#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2004
    Posts
    2
    Rep Power
    0

    regular expressions...


    how do i utilise regular expressions (or other methods) in python to help me to obtain the hostname, ip address and mac address from a file containing entries like the one below (among other things)??

    host malleus {
    hardware ethernet 00:08:02:6a:7e:93;
    fixed-address 10.0.0.88;
    }


    ...many thanks...
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Maybe you could attach a small sample file, say with about 7 entries. Also, it would help if you make the bits you want bold - just so we know what were after - in the post above i.e. something like this?

    Code:
    host malleus {
    hardware ethernet 00:08:02:6a:7e:93;
    fixed-address 10.0.0.88;
    }
    Also if you have any experiance with regex you should give Kodos a go; even if its just for fun.

    http://kodos.sourceforge.net/

    Have fun,

    Mark.
    programming language development: www.netytan.com Hula

  4. #3
  5. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    13
    Assuming the sample represents the correct format here is a possible solution (leaving out file handling):
    Code:
    import re
    
    #some sample data mixed with rubbish
    test_data = '''
    host fred {}
    
     host malleus {
    hardware ethernet 00:08:02:6a:7e:93;
    fixed-address 10.0.0.88;
    }
    ;hdkjahdskjahsdjkha
    #lkjdlakjsd
     host freddy {
    hardware ethernet 00:08:02:6a:7e:93;
    fixed-address 10.0.0.88;
    }
    #kdlkjdlkjdlk
    { rubbish }
    '''
    
    #sub string to match a hex mac address
    mac = r'[a-fA-F0-9]{2}:[a-fA-F0-9]{2}:[a-fA-F0-9]{2}:[a-fA-F0-9]{2}:[a-fA-F0-9]{2}:[a-fA-F0-9]{2}'
    
    #sub-string to macth a decimal ip address
    ip = r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}'
    
    #The comp
    hostmatch = r'host\s+(?P<name>\w+)\s+\{\s+hardware\s+(?P<hardware>\w+)\s+(?P<mac>%s);\s+fixed\-address\s+(?P<ip>%s);\s+\}'%(mac, ip)
    client = re.compile(hostmatch)
    
    #Assume the file is samll enough to read into memory and scan the lot for matches
    for n in client.findall(test_data): 
        print n
    If the file is to big to read into memory then I suggest you read the file into a string until you find a '}' then try to match the sub-string and continue through the file until the end. In this case you can used the named groups to pull out the elements

    Something like;
    Code:
    sub = client.search(sub_string)
    if sub:
        print sub.group('ip'),sub.group('mac')
    I sure there are other ways but this seems to work.

    grim
    Last edited by Grim Archon; September 29th, 2004 at 04:11 AM.

IMN logo majestic logo threadwatch logo seochat tools logo