Aktywne dzierzawy DHCP

Command to list assigned dhcp addresses

isc-dhcpd package version 4.3.1 has this command to list leases:

dhcp-lease-list --lease PATH_TO_LEASE_FILE 

This is a simple perl script script that also supports older DHCP releases. You can see a copy in the Debian source code or in the official DHCP distribution (in contrib/ ) as well.

$ perl contrib/dhcp-lease-list.pl --lease /var/db/dhcpd/dhcpd.leases To get manufacturer names please download http://standards.ieee.org/regauth/oui/oui.txt to /usr/local/etc/oui.txt MAC IP hostname valid until manufacturer =============================================================================================== 90:27:e4:f9:9d:d7 192.168.0.182 iMac-de-mac 2015-12-12 01:37:06 -NA- d8:a2:5e:94:40:81 192.168.0.178 foo-2 2015-12-12 01:04:56 -NA- e8:9a:8f:6e:0f:60 192.168.0.127 angela 2015-12-11 23:55:32 -NA- ec:55:f9:c5:f2:55 192.168.0.179 angela 2015-12-11 23:54:56 -NA- f0:4f:7c:3f:9e:dc 192.168.0.183 kindle-1234567 2015-12-11 23:54:31 -NA- f4:ec:38:e2:f9:67 192.168.0.185 -NA- 2015-12-11 23:55:40 -NA- f8:d1:11:b7:5a:62 192.168.0.184 -NA- 2015-12-11 23:57:34 -NA- 

It’s prettier if you download the oui.txt file as suggested, but then the output can get garbled unless you apply the following patch:

--- dhcp-lease-list.pl.orig 2015-12-12 12:30:00.000000000 -0500 +++ dhcp-lease-list.pl 2015-12-12 12:54:31.000000000 -0500 @@ -41,7 +41,7 @@ if (defined $oui) < $manu = join('-', ($_[0] =~ /^(..):(..):(..):/)); $manu = `grep -i '$manu' $oui | cut -f3`; - chomp($manu); + $manu =~ s/^\s+|\s+$//g; >return $manu; @@ -142,7 +142,7 @@ > foreach (@leases) < if ($opt_format eq 'human') < - printf("%-19s%-16s%-15s%-20s%-20s\n", + printf("%-19s%-16s%-14.14s %-20s%-20s\n", $_->, # MAC $_->, # IP address $_->, # hostname 

This patch was submitted upstream as ISC-Bugs #41288 and awaits review.

No, you can only get this information server side from the DHCP server. This information is contained in the DHCP server’s .lease file: /var/lib/dhcpd/dhcpd.leases , if you’re using ISC’s DHCP server.

Example

$ more /var/lib/dhcpd/dhcpd.leases # All times in this file are in UTC (GMT), not your local timezone. This is # not a bug, so please don't ask about it. There is no portable way to # store leases in the local timezone, so please don't request this as a # feature. If this is inconvenient or confusing to you, we sincerely # apologize. Seriously, though - don't ask. # The format of this file is documented in the dhcpd.leases(5) manual page. # This lease file was written by isc-dhcp-V3.0.5-RedHat lease 192.168.1.100 < starts 4 2011/09/22 20:27:28; ends 1 2011/09/26 20:27:28; tstp 1 2011/09/26 20:27:28; binding state free; hardware ethernet 00:1b:77:93:a1:69; uid "\001\000\033w\223\241i"; >. . 

egrep command can be used to get an output:

egrep "lease|hostname|hardware|\>" /var/lib/dhcpd/dhcpd.leases 
lease 192.168.11.10 < hardware ethernet 20:6a:8a:55:19:0a; client-hostname "Maryam-PC"; >lease 192.168.11.7 < hardware ethernet 00:16:ea:51:d3:12; client-hostname "parsoon"; >lease 192.168.11.3 < hardware ethernet 00:17:c4:3f:84:e3; client-hostname "zahra-ubuntu"; >lease 192.168.11.5

Most of the answers above are partial. And to be honest there is no simple solution. 1) You can parse the dhcpd.leases database file and get information on active leases, but you will not get information on any FIXED addresses (assigned by a line like:

Читайте также:  Killing background processes in linux

And this also is not really giving any information on when was the last time a dhcp ack was sent to the machine.

2) on the other hand you can parse the dhcpd.log file to search for ack lines (they look like this):

2017-03-12T08:44:52.421114+01:00, Linuxx, info, dhcpd: DHCPREQUEST for 10.0.0.63 from 68:ab:35:59:9c:a1 via 10.0.0.1 2017-03-12T08:44:52.421174+01:00, Linuxx, info, dhcpd: DHCPACK on 10.0.0.63 to 68:ab:35:59:9c:a1 via 10.0.0.1 

But what you should really do is to do BOTH. First parse the log file, and then update the file with information obtained from dhcpd.leases file with database for missing information like lease start-end etc.

Now: I have played circa 2 full workdays till I have created a solution which creates a HTML table with ALL active leases, both FIXED and dynamic. Here is the code that you can place in your cgi-bin folder or wherever.

#!/usr/bin/perl ##################################################################################### # list dhcpd active leases # - both "fixed" addresses which are normally not placed into leases database # - and dynamically given leases which are present in leases DB # working for isc-dhcpd-server service but should also work for other compatible # dhcpd servers. # produces HTML or CSV list of leases # # written by Marcin Gosiewski, BV Grupa s.c. Poland # based on portions of code by Jason Antman # # to make it work change the $logfilename and $leasedbname below and modify # the regexp in second part of code (see below) to match your log lines format # also you can optionally turn off reverse dns lookup (see below) which speeds up the process # of table creation and is useless unless you have reverse dns populated for # your fixed or dynamic leases # # CHANGELOG: # 2017-03-13: initial version use Socket; use strict; use warnings; no warnings 'uninitialized'; # adjust this to match your files location: both log file and leases # database. We use 2 last log files from logrotate, but you can add as many as you want my @logfilenames = ( "/var/log/LOCALAPP.dhcpd.log.1", "/var/log/LOCALAPP.dhcpd.log" ); my $leasedbname = "/var/lib/dhcp/dhcpd.leases"; my %data = (); # optional, can be modified to produce local time use Time::Local; use POSIX 'strftime'; my $now = time(); # local variables, lease information stored here my $ip=""; my $status=""; my $interface=""; my $sdate=""; # beginning of lease my $stime=""; my $edate=""; # end of lease my $etime=""; my $adate=""; # last update (ACK) sent to requesting server my $atime=""; my $mac=""; my $hostname=""; my $dnsname=""; # reverse dns lookup for host ####################################################################### # first gather data from logfile for all ACK actions ####################################################################### # collect all lines from log files into memory. my @lines = (); my @loglines=(); foreach my $logfilename (@logfilenames) < open LOGFILE, '); #printf "LINES1: " . scalar @loglines . " in " .$logfilename . "\n"; push(@lines, @loglines); close(LOGFILE); > @loglines=(); #printf "TOTAL LINES: " . scalar @lines . "\n"; foreach my $line (@lines) < if ( $line !~ m/dhcpd: DHCPACK/) < next;>#printf "LINE: $line\n"; ############################### # Modify the following line to make regexp capture 6 groups from log line: # 1 - date # 2 - time # 3 - ip # 4 - mac # 5 - hostname if available # 6 - interface #$line =~ m/(^.)T(.).+,\ dhcpd: DHCPACK on (\d\.\d\.\d\.\d) to ((?:[0-9a-f][:-])[0-9a-f].*) via (.+)/; $line =~m/(^.)T(.).+,\ dhcpd: DHCPACK on (\d\.\d\.\d\.\d) to ((?:[0-9a-f][:-])[0-9a-f]) (.*)via (.+)/; # process the input $adate="$1"; $atime="$2"; $ip="$3"; $mac="$4"; $hostname="$5"; $interface="$6"; #add some 'known' facts: $status="ACK"; $sdate=""; #"FOREVER"; $stime=""; $edate=""; $etime=""; #create/update record for this mac_addr #you can add extra check here if the IP address is not duplicated within #ack history and choose only the newer one. $data-> = "$ip"; $data-> = "$status"; $data-> = "$interface"; $data-> = "$adate"; $data-> = "$atime"; $data-> = "$sdate"; $data-> = "$stime"; $data-> = "$edate"; $data-> = "$etime"; $data-> = "$mac"; $data-> = "$hostname"; > #close(LOGFILE); ####################################################################### # gather data from lease database for dynamic addresses # update the records (for existing) or add new records ####################################################################### my $isdata = 0; my $type = ""; #this information is not present in leases database so we just set #it to default values $interface="dhcpd"; $status="ACTIVE"; $adate="-"; $atime=""; open LEASEDB, $leasedbname or die $!; foreach my $line () < chomp($line); $isdata = 1 if $line =~ /^lease /; $isdata = 0 if $line =~ /^>/; if ($isdata) < if ($line =~ /^lease/) < $ip = (split(" ", $line))[1]; >elsif ($line =~ /^ starts/) < ($sdate, $stime) = (split(" ", $line))[2,3]; $sdate =~ s/\//-/g; $stime =~ s/;//; >elsif ($line =~ /^ ends/) < ($type, $edate, $etime) = (split(" ", $line))[1,2,3]; if($type eq "never;") < $edate="forever"; $etime=" "; >else < $edate =~ s/\//-/g; $etime =~ s/;//; >> elsif ($line =~ /^ hardware ethernet/) < $mac = (split(" ", $line))[2]; $mac =~ s/;//; >elsif ($line =~ /^ client-hostname/) < $hostname = (split(/\"/, $line))[1]; >elsif($mac ne "") < #we have parsed the whole record, no more matching entries #data is collected to variables. now push the record. #now let's decide if we are updating the record or creating #new record # check against lease date, do not add expired leases # convert lease end time to local time/date and compare with $now my $y=0; my $m=0; my $d=0; my $H=0; my $M=0; my $S=0; my $edatetime = $now; ($y, $m, $d) = split("-", $edate); ($H, $M, $S) = split(":", $etime); $edatetime = timelocal($S,$M,$H,$d,$m-1,$y); if($edatetime >= $now) < # now check if record exists if(!defined($data->)) < #record does not exist, fill up default data $data-> = "$mac"; $data-> = "$interface"; $data-> = "$ip"; $data-> = "$hostname"; > # record exists, let's check if we should update $data-> = "$status"; $data-> = "$sdate"; $data-> = "$stime"; $data-> = "$edate"; $data-> = "$etime"; $data-> = "$hostname"; #we do NOT update ACK time because we do not have it #do NOT uncomment below #$data-> = "$adate"; #$data-> = "$atime"; > > > > close(LEASEDB); ####################################################################### # sort data ####################################################################### #we sort by IP but you can sort by anything. my @sorted = sort < ($data) cmp ($data) > %data; ####################################################################### # Print out everything to the HTML table ####################################################################### my $hostnamelong=""; printf "Content-type: text/html\n\n"; printf "\n"; printf " \n"; printf "\n"; printf "\n"; printf "\n"; foreach my $key (@sorted) < if($data eq "") < next ; ># BEGIN reverse dns lookup # can optionally turn off reverse dns lookup (comment out below lines) which speeds up the process # of table creation and is useless unless you have reverse dns populated for # your fixed or dynamic leases uncomment single line below instead: # # version without reverse dns lookup: # $hostnamelong = $data; # # version with reverse dns lookup: # BEGIN $dnsname = gethostbyaddr(inet_aton($data), AF_INET); if($data ne "") < $hostnamelong = $data . " | " . $dnsname; > else < $hostnamelong = $dnsname; >$dnsname = ""; # END printf ""; printf ""; printf ""; printf ""; printf ""; printf ""; printf ""; printf ""; printf "\n"; > printf "
IPStatusInterfaceLease timeACK timeMacHost
" . $data ."" . $data ."" . $data ."" . $data . " " . $data ." - "; printf $data . " " . $data ."" . $data . " " . $data . "" . $data ."" . $hostnamelong ."
\n"; printf "\n"; # END of programm

Please note, that: 1) the above script needs slight modification before running in YOUR environment, you have to modify the files locations, and one regex depending on your log file format. See comment in script. 2) the above script is not checking whether the IP is not repeated in ACK table, if 2 different machines got the same address within last days. This is by design (what I personally needed to see each mac address which was present in my network during last days) — you can easily modify it, there is a ready section for this in code, just add one condition.

Читайте также:  Delphi add linux platform

Источник

Re: выданные dhcp адреса устройств с их маками

Локальный dhcp сервер? В смысле на своей же машине? Тогда смотреть в конфиг.

Или написать простой скрипт, меняющий маки на сетевухе.

Re: выданные dhcp адреса устройств с их маками

Re: выданные dhcp адреса устройств с их маками

прошу прощения за неясность
нужны:
выданный ip
mac и hostname устройства получившего этот ip

Re: выданные dhcp адреса устройств с их маками

Re: выданные dhcp адреса устройств с их маками

dhcpd.leases — тут содержится информация о клиентах, которым DHCP сервис раздаёт IP.

Re: выданные dhcp адреса устройств с их маками

/var/log/messages
Jul 2 03:58:01 gate dhcpd: DHCPDISCOVER from 00:16:76:46:19:7e via eth1
Jul 2 03:58:01 gate dhcpd: DHCPOFFER on 192.168.0.200 to 00:16:76:46:19:7e via eth1

Похожие темы

  • Форум DHCP не раздает адреса Win98 (2001)
  • Форум Centos 7. DNS. DHCP (2016)
  • Форум dnsmasq-dhcp выдаёт двум машинам один ip адрес (2023)
  • Форум dhcp сервер не раздает адреса 10.26.3. (2007)
  • Форум dhcp и hostname (2017)
  • Форум dhcp сервер — работает только ручное распределение IP адресов (2020)
  • Форум DHCP не раздает адреса для Windows (2001)
  • Форум Настройка DHCP (привязать к мак адресу) (2003)
  • Форум Iiptables и dhcp (2008)
  • Форум Archlinux. Проблемы с DHCP. (2008)

Источник

Оцените статью
Adblock
detector