Friday, July 22, 2011

who have been my nfs clients?

sure, you can plod through /var/log/messages to maybe see who has connected to your nfs server. why not just look at rmtab?

#!/usr/bin/perl

$|=1;

use strict;

use Net::DNS;
use Data::Dumper;

use constant DEBUG=>0;

sub logmsg
{
    print STDERR scalar(localtime), " $0 pid $$ ", @_, "\n";
}

sub dnsLookup($)
{
    chomp();
    my $queryname = shift;
    logmsg("Checking queryname $queryname...") if DEBUG==1;
    my $res   = Net::DNS::Resolver->new;
    my $query = $res->search($queryname);
    if ($query) {
        foreach my $rr ($query->answer) {
            if ($rr->type eq "A" ) {
              return $rr->address;
            } 
            if ($rr->type eq "PTR") {
              return $rr->ptrdname;
            }
        }
    } else {
        return $res->errorstring;
    }
}

open fRMTAB, "/var/lib/nfs/rmtab" || die "Unable to open rmtab for reading: $!";
while() {
        my($host,$mount,$count) = split /:/, $_;
        if ( $host =~ m/\d+\.\d+\.\d+\.\d+/ ) {
                print dnsLookup($host), "\n";
        }
}
close fRMTAB;

No comments: