Archive DHCP Bindings from Cisco Router using EEM
This recipe describes a simple method to send DHCP bindings from a Cisco router to a remote TFTP server running on Windows. A batch file renanes and date-stamps the file and another batch file removes the archived files after a retention period:
EEM SCRIPT ON CISCO ROUTER:
(Note: the cron entry sends the file every 5 minutes)
event manager session cli username “your_username” (NOTE: this is required if tacacs+ is running on the router)
event manager applet copy-dhcp-bindings
event timer cron cron-entry “*/5 * * * * ”
action 0.5 cli command “enable”
action 1.0 cli command “show ip dhcp binding | redirect tftp://[ip_address]/bindings.txt”
!
endBATCH FILE ON TFTP SERVER TO DATE-STAMP FILES RECEIVED:
@echo off
:loop
for /f “delims=” %%a in (‘wmic OS Get localdatetime ^| find “.”‘) do set “dt=%%a”
set “YY=%dt:~2,2%”
set “YYYY=%dt:~0,4%”
set “MM=%dt:~4,2%”
set “DD=%dt:~6,2%”
set “HH=%dt:~8,2%”
set “Min=%dt:~10,2%”
set “Sec=%dt:~12,2%”set datestamp=%YYYY%%MM%%DD%
set timestamp=%HH%%Min%%Sec%
set fullstamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%ren “bindings.txt” “bindings – %fullstamp%.txt”
powershell -command “Start-Sleep -s 5
goto loopBATCH FILE CONTENTS – RETENTION PERIOD OF 3 DAYS
@echo off
:loop
powershell -command “Start-Sleep -s 259200
del bindings*.*
goto loop