[2966] | 1 | #!/usr/bin/perl -w
|
---|
| 2 |
|
---|
| 3 | # Syntax: find-net <NFS Server>
|
---|
| 4 |
|
---|
| 5 | use strict;
|
---|
| 6 | use Socket;
|
---|
| 7 | use Data::Dumper;
|
---|
| 8 | #use ProjectBuilder::Base;
|
---|
| 9 |
|
---|
| 10 | my $nfsip = inet_ntoa(scalar gethostbyname($ARGV[0] || 'localhost'));
|
---|
| 11 | print "NFS IP: $nfsip\n";
|
---|
| 12 |
|
---|
| 13 | my %if;
|
---|
| 14 | my $if = \%if;
|
---|
| 15 |
|
---|
| 16 | my $curdev = "";
|
---|
| 17 | open(IP,"ip addr |") || die "Unable to read IP config\n";
|
---|
| 18 | while(<IP>) {
|
---|
| 19 | # Remove duplicate spaces
|
---|
| 20 | $_ =~ s/\s+/ /g;
|
---|
| 21 | #print "Line: $_\n";
|
---|
| 22 | # Check for a new interface
|
---|
| 23 | my $tmp;
|
---|
| 24 | if (/^[0-9]+:/) {
|
---|
| 25 | my $dev;
|
---|
| 26 | my $rank;
|
---|
| 27 | ($rank,$dev,$tmp) = split(/:/);
|
---|
| 28 | #print "$rank,$dev,$tmp";
|
---|
| 29 | $dev =~ s/\s*//;
|
---|
| 30 | $if->{$dev}->{'if'} = $dev;
|
---|
| 31 | $curdev = $dev;
|
---|
| 32 | }
|
---|
| 33 | ($tmp,$tmp,$if->{$curdev}->{'mac'},$tmp) = split(/ /) if (/^ link/);
|
---|
| 34 | ($tmp,$tmp,$if->{$curdev}->{'cidr'},$tmp) = split(/ /) if (/^ inet/);
|
---|
| 35 | }
|
---|
| 36 | close(IP);
|
---|
| 37 |
|
---|
| 38 | # Ideas Taken from http://nixcraft.com/shell-scripting/11398-simple-ipcalc-perl-script.html
|
---|
| 39 | foreach my $dev (keys %if) {
|
---|
| 40 | next if (not defined $if->{$dev}->{'cidr'});
|
---|
| 41 | ($if->{$dev}->{'ip'},my $cidr) = split(/\//,$if->{$dev}->{'cidr'});
|
---|
| 42 |
|
---|
| 43 | $if->{$dev}->{'nm'} = cvt_bits_mask($cidr);
|
---|
| 44 |
|
---|
| 45 | my ($ipaddress) = unpack("N",pack( "C4",split(/\./,$if->{$dev}->{'ip'})));
|
---|
| 46 | my ($netmask) = unpack( "N", pack( "C4",split(/\./,$if->{$dev}->{'nm'})));
|
---|
| 47 |
|
---|
| 48 | # Calculate network address by logical AND operation of addr & netmask
|
---|
| 49 | # and convert network address to IP address format
|
---|
| 50 | $if->{$dev}->{'net'} = join(".",unpack("C4",pack("N",($ipaddress & $netmask))));
|
---|
| 51 |
|
---|
| 52 | # Calculate broadcase address by inverting the netmask
|
---|
| 53 | # and do a logical or with network address
|
---|
| 54 | $if->{$dev}->{'bcast'} = join(".",unpack("C4",pack("N",($ipaddress & $netmask)+(~ $netmask))));
|
---|
| 55 | }
|
---|
| 56 | print "IP: ".Dumper($if)."\n";
|
---|
| 57 |
|
---|
| 58 | # From http://icmp.ru/man/cisco/cookbook/ciscockbk-CHP-5-SECT-3.htm
|
---|
| 59 | sub cvt_bits_mask() {
|
---|
| 60 |
|
---|
| 61 | my ($bits) = @_;
|
---|
| 62 | my $a = 0;
|
---|
| 63 | my $b = 0;
|
---|
| 64 | my $c = 0;
|
---|
| 65 | my $d = 0;
|
---|
| 66 |
|
---|
| 67 | if ($bits <= 8 ) {
|
---|
| 68 | $a = bits_to_dec($bits);
|
---|
| 69 | } else {
|
---|
| 70 | $a = 255;
|
---|
| 71 | if ($bits <= 16 ) {
|
---|
| 72 | $b = bits_to_dec($bits-8);
|
---|
| 73 | } else {
|
---|
| 74 | $b=255;
|
---|
| 75 | if ($bits <= 24 ) {
|
---|
| 76 | $c = bits_to_dec($bits-16);
|
---|
| 77 | } else {
|
---|
| 78 | $c=255;
|
---|
| 79 | if ($bits <= 32 ) {
|
---|
| 80 | $d = bits_to_dec($bits-24);
|
---|
| 81 | } else {
|
---|
| 82 | print "invalid bit count\n";
|
---|
| 83 | exit();
|
---|
| 84 | }
|
---|
| 85 | }
|
---|
| 86 | }
|
---|
| 87 | }
|
---|
| 88 | return ($a.".".$b.".".$c.".".$d);
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | sub bits_to_dec() {
|
---|
| 92 | my ($bits) = @_;
|
---|
| 93 |
|
---|
| 94 | if ($bits == 0 ) { return 0; }
|
---|
| 95 | if ($bits == 1 ) { return 128; }
|
---|
| 96 | if ($bits == 2 ) { return 192; }
|
---|
| 97 | if ($bits == 3 ) { return 224; }
|
---|
| 98 | if ($bits == 4 ) { return 240; }
|
---|
| 99 | if ($bits == 5 ) { return 248; }
|
---|
| 100 | if ($bits == 6 ) { return 252; }
|
---|
| 101 | if ($bits == 7 ) { return 254; }
|
---|
| 102 | if ($bits == 8 ) { return 255; }
|
---|
| 103 | }
|
---|