Network – How to Show History of IP Addresses Assigned to iMac

Network

Is there a way to view the list of IP addresses that were assigned to my iMac over a period of time (e.g., the last 30 days)? I'd like to get this information from the iMac itself if possible.

Best Answer

By default your mac won't keep a history of DHCP address and times. I put together a launchd and script that should do the trick if you must have client side records for DHCP times and ips.

launcd plist called com.local.DHCP-History.plist that needs to be placed in /Library/LaunchDaemons/

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Disabled</key>
    <false/>
    <key>Label</key>
    <string>DHCP_History</string>
    <key>Program</key>
    <string>/usr/bin/DHCPhistoryLog.sh</string>
    <key>WatchPaths</key>
    <array>
        <string>/private/var/db/dhcpclient/leases/*</string>
    </array>
    <key>WorkingDirectory</key>
    <string>/usr/bin/</string>
</dict>
</plist>

Here is the script it will run, I put it in /usr/bin/ but you can put where you want just make sure to change the environment path in the launch daemon. You will have to create a folder called DHCP_History in /private/var/log/ to create the log files in.

DHCP_History.sh

#!/bin/bash
#
# tron_jones 09-10-14

# Create a log for DHCP changes and put in new folder with date
dateVar=`date` 
mkdir /private/var/log/DHCP_History/"$dateVar" 
path="/private/var/db/dhcpclient/leases/"
logPath="/private/var/log/DHCP_History/"
for i in `ls /private/var/db/dhcpclient/leases/`
do
    cp "${path}${i}" "${logPath}${dateVar}"/DHCP_ChangeHistory_"${i}".log
done

Make sure that the script is executable and owned by root:wheel. Run these commands to do that:

sudo chmod +x /usr/bin/DHCP_History.sh

sudo chown root:wheel /usr/bin/DHCP_History.sh

Now every time DHCP gets renewed or changes it will create a file called DHCP_ChangeHistory.log inside /private/var/log/DHCP_History/thedate/