Get DHCP leases from a PiHole DNS server


#1

If you run a PiHole server that serves DHCP, you can easily create a UDTable that displays your current leases.

This Table refreshes every 60 seconds:

You’ll need ssh access. Replace SSH_USER and PIHOLE_IP as appropriate.

New-UDTable  -Title "DHCP Leases" -Headers @('Hostname', 'IP Address', 'MAC Address')  -AutoRefresh -RefreshInterval 60 -Endpoint {
    $Leases = (ssh -l SSH_USER PIHOLE_IP "cat /etc/pihole/dhcp.leases;exit")
    
    $metrics = Foreach ($obj in $Leases) {
        [PSCustomObject]@{
            'Hostname'    = $obj.Split(" ")[3]
            'IP Address'  = $obj.Split(" ")[2]
            'MAC Address' = $obj.Split(" ")[1]
        }
    }

    $metrics | Out-UDTableData -Property @('Hostname', 'IP Address', 'MAC Address')
}

Produces:

dhcpleases