zit11 | Je me réponds
En gros, y a pas de gestion rapide...Mais avec 2/3 scripts on peut s'en sortir.
Voici ce que j'ai trouvé :
Ce petit script en perl permet de récuperer les infos de dhcpd.leases et d'avoir un ptit récapitulatif du bail, du constructeur (suivant l'adresse mac) avec le fichier oui.txt qui contient la liste des constructeurs.
script perl :
Code :
- #!/usr/bin/perl
- #
- # Shows current leases.
- #
- # THIS SCRIPT IS PUBLIC DOMAIN, NO RIGHTS RESERVED!
- # 2008-07-13, Christian Hammers <ch@lathspell.de>
- #
- use strict;
- use warnings;
- use POSIX qw(strftime);
- my $LEASES = '/var/lib/dhcp3/dhcpd.leases';
- my @all_leases;
- my @leases;
- my @OUIS = ('/usr/share/misc/oui.txt', '/usr/local/etc/oui.txt');
- my $OUI_URL = 'http://standards.ieee.org/regauth/oui/oui.txt';
- my $oui;
- my %data;
- my $opt_format = 'human';
- my $opt_keep = 'active';
- ## Return manufactorer name for specified MAC address (aa:bb:cc:dd:ee:ff).
- sub get_manufactorer_for_mac($) {
- my $manu = "";
- if (defined $oui) {
- $manu = join('-', ($_[0] =~ /^(..):(..):(..):/));
- $manu = `grep -i '^$manu' $oui | cut -f3`;
- chomp($manu);
- }
- return $manu;
- }
- ## Read oui.txt or print warning.
- sub check_oui_file() {
- for my $oui_cand (@OUIS) {
- if ( -r $oui_cand) {
- $oui = $oui_cand;
- last;
- }
- }
- print(STDERR "Please download $OUI_URL to /usr/local/etc/oui.txt\n" ) if not defined $oui;
- }
- ## Read current leases file into array.
- sub read_dhcpd_leases() {
- open(F, $LEASES) or die("Cannot open $LEASES: $!" );
- my $content = join('', <F> );
- close(F);
- @all_leases = split(/lease/, $content);
- }
- ## Add manufactor name and sort out obsolet assignements.
- sub process_leases() {
- my $gm_now = strftime("%Y/%m/%d %H:%M:%S", gmtime());
- my %tmp_leases; # for sorting and filtering
- # parse entries
- foreach my $lease (@all_leases) {
- # skip invalid lines
- next if not ($lease =~ /^\s+([\.\d]+)\s+{.*starts \d+ ([\/\d\ \:]+);.*ends \d+ ([\/\d\ \:]+);.*ethernet ([a-f0-9:]+);/s);
- # skip outdated lines
- next if ($opt_keep eq 'active' and $3 lt $gm_now);
- my $mac = $4;
- my $date_end = $3;
- my %entry = (
- 'ip' => $1,
- 'date_begin' => $2,
- 'date_end' => $date_end,
- 'mac' => $mac,
- 'manu' => get_manufactorer_for_mac($mac),
- );
- $entry{'date_begin'} =~ s#\/#-#g; # long live ISO 8601
- $entry{'date_end'} =~ s#\/#-#g;
- if ($opt_keep eq 'all') {
- push(@leases, \%entry);
- } elsif (not defined $tmp_leases{$mac} or $tmp_leases{$mac}{'date_end'} gt $date_end) {
- $tmp_leases{$mac} = \%entry;
- }
- }
- # In case we used the hash to filtered
- if (%tmp_leases) {
- foreach (sort keys %tmp_leases) {
- my $h = $tmp_leases{$_};
- push(@leases, $h);
- }
- }
- }
- # Output all valid leases.
- sub output_leases() {
- if ($opt_format eq 'human') {
- print("# MAC IP valid until manufactorer\n" );
- }
- foreach (@leases) {
- if ($opt_format eq 'human') {
- printf("%s %s %s %s\n",
- $_->{'mac'}, # MAC
- $_->{'ip'}, # IP address
- $_->{'date_end'}, # Date
- $_->{'manu'}); # manufactor name
- } else {
- printf("MAC %s IP %s BEGIN %s END %s MANUFACTORER %s\n",
- $_->{'mac'},
- $_->{'ip'},
- $_->{'date_begin'},
- $_->{'date_end'},
- $_->{'manu'});
- }
- }
- }
- # Commandline Processing.
- sub cli_processing() {
- while (my $arg = shift(@ARGV)) {
- if ($arg eq '--help') {
- print(
- "Prints active DHCP leases.\n\n".
- "Usage: $0 [options]\n".
- " --help shows this help\n".
- " --parsable machine readable output with full dates\n".
- " --last prints the last (even if end<now) entry for every MAC\n".
- " --all prints all entries i.e. more than one per MAC\n".
- "\n" );
- exit(0);
- } elsif ($arg eq '--parsable') {
- $opt_format = 'parsable';
- } elsif ($arg eq '--last') {
- $opt_keep = 'last';
- } elsif ($arg eq '--all') {
- $opt_keep = 'all';
- } else {
- die("Unknown option $arg" );
- }
- }
- }
- #
- # main()
- #
- cli_processing();
- check_oui_file();
- read_dhcpd_leases();
- process_leases();
- output_leases();
|
- Après un autre petit script qui exécute ce script et qui envoie la sortie de celui ci en mail:
Code :
- #!/bin/sh
- perl dhcp.perl > out.txt
- sleep 10
- mail -s "Baux DHCP" monadresse@mail.com < out.txt
|
Voila, c'est du bidouillage mais ça fonctionne bien si ça peut servir à quelqu'un ---------------
Je suis pas Superman, je suis Super-vagabond, mais toi t’es Super-pomme. Tu es très gouteuse, tu es très... biologique, très naturelle ! Tu es la pomme de ma vie.
|