source: MondoRescue/branches/3.0/mindi/mr-find-net@ 2966

Last change on this file since 2966 was 2966, checked in by Bruno Cornec, 12 years ago

r4567@cabanilles: bruno | 2012-03-15 02:50:46 +0100

  • Prepare a new script to compute IP addresses
  • Property svn:executable set to *
File size: 2.5 KB
Line 
1#!/usr/bin/perl -w
2
3# Syntax: find-net <NFS Server>
4
5use strict;
6use Socket;
7use Data::Dumper;
8#use ProjectBuilder::Base;
9
10my $nfsip = inet_ntoa(scalar gethostbyname($ARGV[0] || 'localhost'));
11print "NFS IP: $nfsip\n";
12
13my %if;
14my $if = \%if;
15
16my $curdev = "";
17open(IP,"ip addr |") || die "Unable to read IP config\n";
18while(<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}
36close(IP);
37
38# Ideas Taken from http://nixcraft.com/shell-scripting/11398-simple-ipcalc-perl-script.html
39foreach 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}
56print "IP: ".Dumper($if)."\n";
57
58# From http://icmp.ru/man/cisco/cookbook/ciscockbk-CHP-5-SECT-3.htm
59sub cvt_bits_mask() {
60
61my ($bits) = @_;
62my $a = 0;
63my $b = 0;
64my $c = 0;
65my $d = 0;
66
67if ($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}
88return ($a.".".$b.".".$c.".".$d);
89}
90
91sub bits_to_dec() {
92my ($bits) = @_;
93
94if ($bits == 0 ) { return 0; }
95if ($bits == 1 ) { return 128; }
96if ($bits == 2 ) { return 192; }
97if ($bits == 3 ) { return 224; }
98if ($bits == 4 ) { return 240; }
99if ($bits == 5 ) { return 248; }
100if ($bits == 6 ) { return 252; }
101if ($bits == 7 ) { return 254; }
102if ($bits == 8 ) { return 255; }
103}
Note: See TracBrowser for help on using the repository browser.