Skip to content
Advertisement

How to parse a particular MAC address from DHCP lease file in Openwrt?

I am trying to write a script to parse some of devices from DHCP lease file in Openwrt with corresponding addresses . I am maintain a list with some vendor MAC address . When i connect a device to the router , i want to fetch the IP address ,MAC address and Name of that device using the vendor MAC from DHCP lease file .

For example , if i have maintained list of Vendor MAC address like

MAC LIST =  {"00:01:0A","00:00:1A","00:00:39"}
where ,
    00:01:0A - CISCO
    00:00:1A - AMD  
    00:00:39 - Toshiba TOSHIBA CORPORATION

and in Openwrt dhcp lease file contain different device for example:

root@OpenWrt:/# cat /tmp/dhcp.leases

1568953482  70:B3:D5:14:D0:31 192.168.3.51 device1 01:70:B3:D5:14:D0:31
2867821468  38:B8:EB:10:00:22 192.168.5.93 device2 01:38:B8:EB:10:00:22
8984532872  00:01:0A:33:11:33 192.168.5.44 CISCOee 01:00:01:0A:33:11:33

Where, 2nd column - MAC address , 3rd column - IP address and 4th column- Name

Here i am getting a CISCO device with a MAC address starting from 00:01:0A . I want to write a bash script in openwrt/Unix to fetch the corresponding IP address ,name and MAC address of all the Device with respect to the MAC LIST from /tmp/dhcp.leases file . If no device found with reset to MAC LIST , the script should return NULL . How can i parse this Address list using script ? Any suggestions ?

update :

I want to compare the first 3 digit of Vendor MAC address with dhcp leases file . For example my list contain following vendor MAC address (3 digit) in MAC.txt file :

/usr/MAC.txt
---------

     00:01:0A
     00:00:1A
     00:00:39

and the dhcp.leases contain :

root@OpenWrt:/# cat /tmp/dhcp.leases

1568953482  70:B3:D5:14:D0:31 192.168.3.51 device1 01:70:B3:D5:14:D0:31
2867821468  38:B8:EB:10:00:22 192.168.5.93 device2 01:38:B8:EB:10:00:22
8984532873  00:01:0A:33:11:33 192.168.5.44 CISCOee1 01:00:01:0A:33:11:33
8984532874  00:01:0A:34:12:34 192.168.5.44 CISCOee2 01:00:01:0A:34:12:34

Now i want to compare dhcp leases file MAC address from MAC.txt file with dhcp.leases file . If first 3 digit matching , then i want to return the IP address , MAC address and Name of matching device .

Sample output :

         00:01:0A:33:11:33 192.168.5.44 CISCOee1
         00:01:0A:34:12:34 192.168.5.44 CISCOee2

if nothing found send NULL as output .

Advertisement

Answer

Not sure if this is what you are looking for but:

MAC file contents:

MAC LIST =  {"00:01:0A","00:00:1A","00:00:39"}
where ,
   00:01:0A - CISCO
   00:00:1A - AMD  
   00:00:39 - Toshiba TOSHIBA CORPORATION 

Solution:

awk -F" '/MAC LIST/ { for (i=2;i<=NF-1;i++) { maccie=gensub(",","","g",$i);if ( maccie != "") { macs[maccie]="" } } } FNR==1 && NR != 1 { STRT=1 } STRT==1 { for (i in macs) { FS=" ";if ($2 ~ i) { print $2" - "$3;found[i]=1 } } } END { for (i in macs) { if ( found[i]!=1 ) { print i" - NULL"} } }' MAC dhcp.leases

Output

00:01:0A:33:11:33 - 192.168.5.44
00:00:1A - NULL
00:00:39 - NULL

Here we get awk to process both files MAC (the maintained list) and dhcp.leases. We set the delimited to be ” and then build an array of the mac addresses placing them in “macs” when the string “MAC LIST” is encountered in the line. Once the we reach the dhcp.leases file (FNR – file number record is 1 but number record of both files is not one) we set a variable STRT=1 to signify the processing of the dhcp.leases file. When STRT=1 (we are in the dhcp.leases file) we change the field delimiter (FS) to ” ” and loop through each mac address in the macs array pattern matching it against the 2nd delimited piece of data on the line (the full MAC address) If it matches, we print out the data and set an array “found” with the mac address. We finally then loop through each mac address in “mac” again checking against the array “found”. If an entry in found exists ( equals 1), ignore, otherwise print NULL.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement