I'm running a DHCP server on my FreeBSD PC and I want to get the list of connected client on my network but I don't know how. Can someone tell me the best way to do it?
Best Regards
Ryuu
Best Regards
Ryuu
ryuusoultaker said:I'm running a DHCP server on my FreeBSD PC and I want to get the list of connected client on my network but I don't know how. Can someone tell me the best way to do it?
Ryuu
plamaiziere said:You will find the leases in /var/something (don't remember exactly) with their status.
regards
Parse the entire file.ryuusoultaker said:I checked the leases file at /var/db/dhcpd/dhcpd.lease but it only provide me the information about last leased IP. I need the list of leased IP that still online so I can see how much client that connected from my BSD machine if it possible..
#!/bin/tcsh
## listleases.sh
## Displays a list of active DHCP leases
## NOTE: requires fping port to use
## Get pool(s) from /usr/local/etc/dhcpd.conf
foreach POOL (`grep range /usr/local/etc/dhcpd.conf | awk '{print $2"_"$3}' | sed 's/;//g'`)
set POOLSET = `echo $POOL | sed 's/_/ /g'`
echo "DHCP Pool: $POOLSET"
## use fping to find active IPs in the dhcp pool
foreach ENTRY (`fping -a -g $POOLSET`)
set ENTRYARP = `arp $ENTRY | awk '{print $4}'`
set ENTRYUID = `grep -C7 "lease $ENTRY" /var/db/dhcpd/dhcpd.leases | grep -C3 "$ENTRYARP" | grep hostname | tail -n 1`
echo "$ENTRY | $ENTRYARP | $ENTRYUID"
end
end
SirDice said:Parse the entire file.
pboehmer said:OK, I spent a couple of minutes and concocted this. This script works with the isc42-dhcp-server port, and should work with the other versions. YMMV on other DHCP server software. I extract the pool ranges (if you have multiple pools) from /usr/local/etc/dhcpd.conf and use fping (/usr/ports/net/fping) to list the active addresses. I use arp to get the MAC of the active IPs. From there, I just grep out the hostname from the last matching IP and MAC address in /var/db/dhcp/dhcpd.leases. Its not exactly streamline, but it seems to work.
Code:#!/bin/tcsh ## listleases.sh ## Displays a list of active DHCP leases ## NOTE: requires fping port to use ## Get pool(s) from /usr/local/etc/dhcpd.conf foreach POOL (`grep range /usr/local/etc/dhcpd.conf | awk '{print $2"_"$3}' | sed 's/;//g'`) set POOLSET = `echo $POOL | sed 's/_/ /g'` echo "DHCP Pool: $POOLSET" ## use fping to find active IPs in the dhcp pool foreach ENTRY (`fping -a -g $POOLSET`) set ENTRYARP = `arp $ENTRY | awk '{print $4}'` set ENTRYUID = `grep -C7 "lease $ENTRY" /var/db/dhcpd/dhcpd.leases | grep -C3 "$ENTRYARP" | grep hostname | tail -n 1` echo "$ENTRY | $ENTRYARP | $ENTRYUID" end end
I hope someone finds this useful.
#!/usr/bin/perl
$now = time();
use Time::Local;
open(LEASE, "/var/db/dhcpd/dhcpd.leases");
foreach $line (<LEASE>) {
chomp($line);
$data = 1 if $line =~ /^lease /;
$data = 0 if $line =~ /^}/;
if ($data) {
if ($line =~ /^lease/) {
$ip = (split(" ", $line))[1];
} elsif ($line =~ /^ starts/) {
($date, $time) = (split(" ", $line))[2,3];
($y, $m, $d) = split("/", $date);
($H, $M, $S) = split(":", $time);
$start = timelocal($S,$M,$H,$d,$m,$y);
} elsif ($line =~ /^ ends/) {
($date, $time) = (split(" ", $line))[2,3];
($y, $m, $d) = split("/", $date);
($H, $M, $S) = split(":", $time);
$stop = timelocal($S,$M,$H,$d,$m,$y);
} elsif ($line =~ /^ hardware ethernet/) {
$mac = (split(" ", $line))[2];
$mac =~ s/;//;
} elsif ($line =~ /^ client-hostname/) {
$client = (split(/\"/, $line))[1];
}
} else {
print localtime($start) . "\t" . localtime($stop) . "\t$ip\t$mac\t$client\n" if $stop >= $now;
$ip = ""; $start = ""; $stop = ""; $mac = ""; $client = "";
}
}
close(LEASE);
timelocal($S,$M-1,$H,$d,$m,$y)
timelocal($S,$M,$H,$d,$m-1,$y)