1 | #!/usr/bin/env perl
|
---|
2 |
|
---|
3 | # Set some vars
|
---|
4 |
|
---|
5 | # Filter on netmask
|
---|
6 | $MASK = $ARGV[0];
|
---|
7 | # Otherwise return all.
|
---|
8 | if ( ! $MASK ) {
|
---|
9 | $MASK = ".*";
|
---|
10 | }
|
---|
11 |
|
---|
12 | # Print the header
|
---|
13 | print "Node 1 name, Node 1 ip, Node 2 ip, $node 2 name\n";
|
---|
14 |
|
---|
15 | use File::Basename;
|
---|
16 |
|
---|
17 | sub parse_config {
|
---|
18 | # Set some vars;
|
---|
19 | my $workingfile = $_[0];
|
---|
20 | my $id = $_[1];
|
---|
21 |
|
---|
22 | # Undifine config, to avoid polution
|
---|
23 | undef %config;
|
---|
24 |
|
---|
25 | # Load config file
|
---|
26 | do($workingfile);
|
---|
27 |
|
---|
28 | # Run through all the interfaces
|
---|
29 | foreach my $if (keys %config) {
|
---|
30 |
|
---|
31 | # Config file has interface with semicolumn in it, not possible to use these a vars.
|
---|
32 | $ifwithsemicolumn=$if;
|
---|
33 | $if=~s/://g;
|
---|
34 |
|
---|
35 | # Get interface info
|
---|
36 | my $cfg=$config{$ifwithsemicolumn};
|
---|
37 |
|
---|
38 | # Run through all the parameters of the interface
|
---|
39 | while ($cfg) {
|
---|
40 |
|
---|
41 | # Remove newlines
|
---|
42 | $cfg=~s/^([^\n\r]+)[\r\n]*//m;
|
---|
43 |
|
---|
44 | # Get first word from the sentence
|
---|
45 | my $line=$1;
|
---|
46 |
|
---|
47 | # Remove unwanted characters
|
---|
48 | $line=~s/\s*#.*//;
|
---|
49 |
|
---|
50 | # Split key and values
|
---|
51 | if (((my $name, my $value)=split(/=/,$line)) eq 2) {
|
---|
52 |
|
---|
53 | # Create a variable of the splited info
|
---|
54 | my $doit = "\$$if\_$id\{\"$name\"\}=\"$value\";";
|
---|
55 | eval($doit);
|
---|
56 | };
|
---|
57 |
|
---|
58 | # Remove newlines
|
---|
59 | $cfg=~s/[\r\n]*$//m;
|
---|
60 | };
|
---|
61 | };
|
---|
62 | };
|
---|
63 |
|
---|
64 |
|
---|
65 | sub getneighboril {
|
---|
66 | ## I got an IP, just want to know to which node it belongs
|
---|
67 |
|
---|
68 | # Get ip from parent
|
---|
69 | $search=$_[0];
|
---|
70 |
|
---|
71 | # Open the config file dir
|
---|
72 | opendir ($il_hdir, $dir);
|
---|
73 |
|
---|
74 | # Run through all the Nodes in the specified directory.
|
---|
75 | while (my $il_node = readdir($il_hdir)) {
|
---|
76 |
|
---|
77 | # Only Allow Directories which represent Nodes
|
---|
78 | if ($il_node =~ /^CNode/) {
|
---|
79 |
|
---|
80 | # Read config file
|
---|
81 | $file="$il_node/$configfile";
|
---|
82 | parse_config("$file", "il");
|
---|
83 |
|
---|
84 | # Run through all the interfaces in the config file
|
---|
85 | foreach my $il_if (sort keys %config) {
|
---|
86 |
|
---|
87 | # Told you before, can't have semicolumns in variable names
|
---|
88 | $il_if=~s/://g;
|
---|
89 |
|
---|
90 | # Get the ipinfo of the interface
|
---|
91 | $il_ip = getvar($il_if, "IP", "il");
|
---|
92 |
|
---|
93 | # Split the ip and the subnetmask
|
---|
94 | (my $il_ip, my $il_mask) = split(/\//, $il_ip);
|
---|
95 |
|
---|
96 | # Match the current ip of node/if with the ip specified in the search
|
---|
97 | # Match the netmask specified
|
---|
98 | if (($il_ip eq $search) && ($il_mask =~ /$MASK/)) {
|
---|
99 | return $il_node;
|
---|
100 | }
|
---|
101 | }
|
---|
102 | }
|
---|
103 | }
|
---|
104 | }
|
---|
105 |
|
---|
106 |
|
---|
107 |
|
---|
108 |
|
---|
109 | sub getvar {
|
---|
110 | ## Make an var from different variabls
|
---|
111 |
|
---|
112 | # Get var from parent
|
---|
113 | $if=$_[0];
|
---|
114 | $var=$_[1];
|
---|
115 | $id=$_[2];
|
---|
116 |
|
---|
117 | # Make the var, just forgor why this works :-) (yes I made it myself). Didn't specify the value of the var
|
---|
118 | $retval = eval("\$$if\_$id\{\"$var\"\}");
|
---|
119 | return $retval;
|
---|
120 | }
|
---|
121 |
|
---|
122 | sub getneighborip {
|
---|
123 |
|
---|
124 | ## The the ip of the neighboring interface
|
---|
125 |
|
---|
126 | # Get var (ip) from parent
|
---|
127 | $ip=$_[0];
|
---|
128 |
|
---|
129 | # Split ip into oclets
|
---|
130 | (my $oclet1, my $oclet2, my $oclet3, my $oclet4) = split(/\./, $ip);
|
---|
131 |
|
---|
132 | # Mod the 4th oclet
|
---|
133 | $neighbor = $oclet4 % 4;
|
---|
134 |
|
---|
135 | # 0 = network address, 1 is what we want, 2 makes double values, 3 is broadcast address
|
---|
136 | if ($neighbor eq 1) {
|
---|
137 |
|
---|
138 | # Upper the 4th oclet by one (which makes the 2 if the previous comment
|
---|
139 | $oclet4++;
|
---|
140 | }
|
---|
141 |
|
---|
142 | # Make the neightboring ip address
|
---|
143 | $neighbor="$oclet1.$oclet2.$oclet3.$oclet4";
|
---|
144 |
|
---|
145 | # If the neighbor and this ip match, we return zero the block further actions
|
---|
146 | if($neighbor eq $ip) {
|
---|
147 | return NULL;
|
---|
148 | }
|
---|
149 |
|
---|
150 | # Return neighboring ip
|
---|
151 | return $neighbor;
|
---|
152 | }
|
---|
153 |
|
---|
154 | # Get relative path of the genesis config dir
|
---|
155 | $dir=dirname(".");
|
---|
156 |
|
---|
157 | # wleiden.con fconfig file
|
---|
158 | $configfile="wleiden.conf";
|
---|
159 |
|
---|
160 | # Open genesis config directory
|
---|
161 | opendir ($main_hdir, $dir);
|
---|
162 |
|
---|
163 | # Run through all items in this directory
|
---|
164 | while (my $main_node = readdir($main_hdir)) {
|
---|
165 |
|
---|
166 | # Only open config file which have CNode in the name
|
---|
167 | if ($main_node =~ /^CNode/) {
|
---|
168 | $main_file="$main_node/$configfile";
|
---|
169 | parse_config("$main_file", "main");
|
---|
170 |
|
---|
171 | # Run through all the interface in the config file
|
---|
172 | foreach my $main_if (sort keys %config) {
|
---|
173 |
|
---|
174 | # Told you before, can't have semicolumns in variable names
|
---|
175 | $main_if=~s/://g;
|
---|
176 |
|
---|
177 | # Get the ipinfo of the interface
|
---|
178 | $main_ip = getvar($main_if, "IP", "main");
|
---|
179 |
|
---|
180 | # Get the interface type
|
---|
181 | $main_extra_type = getvar($main_if, "EXTRA_TYPE", "main");
|
---|
182 |
|
---|
183 | # Seperate ip and subnet mask
|
---|
184 | (my $main_ip, my $main_mask) = split(/\//, $main_ip);
|
---|
185 |
|
---|
186 | # We only want /30 netmasks and eth2wifibridges
|
---|
187 | if(($main_mask eq 30) && ($main_extra_type =~ /eth2wifibridge/i)) {
|
---|
188 |
|
---|
189 | # Get the neighboring ip
|
---|
190 | $main_neighbor = getneighborip($main_ip);
|
---|
191 |
|
---|
192 | # Disregard if empty.
|
---|
193 | if($main_neighbor) {
|
---|
194 |
|
---|
195 | # Search the name of the neighboring Node
|
---|
196 | $main_nodename = getneighboril($main_neighbor);
|
---|
197 |
|
---|
198 | # Can be empty for valid reasons, but needs the disregarded
|
---|
199 | if ($main_nodename) {
|
---|
200 |
|
---|
201 | # Print the information we have collected
|
---|
202 | print "$main_node,$main_ip,$main_neighbor,$main_nodename\n";
|
---|
203 | }
|
---|
204 | }
|
---|
205 | }
|
---|
206 | }
|
---|
207 | }
|
---|
208 | }
|
---|
209 |
|
---|
210 | exit 0
|
---|