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

JavaScript

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

JavaScript

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 :

JavaScript

and the dhcp.leases contain :

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

JavaScript

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 .

JavaScript

if nothing found send NULL as output .

Advertisement

Answer

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

MAC file contents:

JavaScript

Solution:

JavaScript

Output

JavaScript

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