rude-config-maker 3.28 KB
Newer Older
1
2
3
4
5
6
#!/usr/bin/perl
# make rude config files from host lists
#   format : src dst1 dst2 ...

my $target;
my $list='mp-list.txt';
7
my $default_port=10001;
8
my $index;  # the index for event records
9
my $rude_port=3100; # start local port number udp
10
my $max_hops=30;
11
12

use Getopt::Long;
13
14
15
16
@opts=( 'target=s' => \$target, 'list=s' => \$list, 'index=s' => \$index, 'rude_start=i' => \$rude_port,
	'max_hops=i' => \$max_hops);
my $usage="usage: $0 [ -target <target host> ] [-list <mp-list.txt>] -index <index> config-file 
-rude_start <3001> --max_hops <n> (30)\n";
17
18
19

GetOptions(@opts) || die $usage;
die $usage if !$index;
20
21
22

open LIST, "<$list" || die "Could not open $list : $!\n";
while(<LIST>){
23
    next if /\s*#/; # comment
24
    ($name, $uid, $dns, $ip, $port, $crude_port, $ssh_port, $max_hop)=split;
25
26
    $ip{$name}=$ip;
    $dns{$name}=$dns;
27
    $port{$name}=$port;
28
    $crude_port{$name}=$crude_port if $crude_port;
29
    $max_hop{$name}= $max_hop || $max_hops_default;
30
}
31
32
33
if ($target && ! $port{$target} ){
    $port{$target} = $default_port;
}
34
35
36

use Socket;
while(<>){
37
    next if /\s*#/; # comment
38
39
40
41
42
43
44
    ($src, @dst)=split;
    foreach $dst(@dst){
	$peers{$src, $dst}++;
	$peers{$dst, $src}++;
    }
}

45

46
my $flow_no=1;
47
48
my $rude; # fd to write config
my $start; # fd to write config
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
my $outdir;
my $gotit=0;

foreach $peer ( sort(keys %peers)){
    my ($src, $dst)=split(/$;/,$peer);

    if ( $target ){
        if ($target eq $src){ # just write config for target to stdout
	    $gotit=1;
	    $outdir=".";
	} else { # avoid writing for other targets
	    $gotit=0;
	}
    } else {
	$gotit=1;
	$outdir=$src;
    }
    if ($psrc ne $src){
	if ( $gotit){
	    if ( $psrc){
69
		close $rude;
70
71
72
		$flow_no=1;
	    }
	    mkdir "$outdir" if ! -d $outdir;
73
74
	    open $rude, ">$outdir/rude.cfg" || die "Could not open ${outdir}/rude.cfg : $!";
	    print $rude "START NOW\n";
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
	}
    }

    $ip=$ip{$dst};
    if ( !$ip){
	if ( $dns{$dst} ){
	    $ipn=gethostbyname($dns{$dst});
	} else {
	    $ipn=gethostbyname($dst);
	}
	if ($ipn){
	    $ip=inet_ntoa($ipn);
	}
    }
    if ($gotit){
	if ($ip ){
91
	    printf $rude "0 %d ON %d %s:%s CONSTANT 100 64\n", $flow_no, $rude_port + $flow_no, $ip,
92
		$port{$dst} || $default_port;;
93
	    printf $rude "90010000 %d OFF\n", $flow_no; # varer en dag +10 sek skuddr
94
95
96
97
98
99
100
101
102
103
104
	    @$tracetarget{$src} = () if ! $tracetarget{$src};
	    push (@{$tracetarget{$src}}, $ip );
	} else {
	    warn "### Could not find IP for $dst, $ip";
	}
    }
    $psrc=$src;
    $flow_no++;
}

close RUDE;
105
106
close $rude;

107

108
109
foreach $name ( keys %port){
    if ( (! $target) || ( $target eq $name ) ){
Olav Kvittem's avatar
Olav Kvittem committed
110
	$outdir=$name;
111
	if ( -d $outdir ){
112
	    open ( $start, ">$outdir/parms.cfg") || die "Could not open ${outdir}/parms.cfg : $!" ;
113
	    printf $start "index=%s\n", $index ;
Olav Kvittem's avatar
Olav Kvittem committed
114
	    my $crude_port= $crude_port{$name} || $port{$name} || $default_port;
Olav Kvittem's avatar
Olav Kvittem committed
115
	    printf $start "crude_port=%s\n", $crude_port;
116
	    printf $start "max_hops=%s\n", $max_hop{$name};
117
	    # printf $start "%s", $startcfg; 
118
119
	    close $start;
	}
120
121
122
    }
    
}
123
124
125
126
127
128
129
130
131

foreach $src ( keys %tracetarget ){
    next if $target and $src ne $target;
    if ( $target){
	next if $src ne $target;
	$outdir=".";
    } else {
	$outdir=$src;
    }
132
133
134
135
136
    if ( -d $outdir ){
	open TRACE, ">$outdir/trace.cfg" || die "Could not open $outdir/start.cfg : $!" ;
	print TRACE join("\n", @{$tracetarget{$src}} );
	close TRACE;
    }
137
138
}

139