source: genesis/nodes/get-a-ils.pl@ 8176

Last change on this file since 8176 was 8161, checked in by richardvm, 15 years ago

A script to filter (A/\/30) interlinks

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