| 1 |
#!/usr/bin/env perl |
| 2 |
# |
| 3 |
# Copyright (C) 2024 Linutronix GmbH |
| 4 |
# Author Kurt Kanzenbach <[email protected]> |
| 5 |
# |
| 6 |
# SPDX-License-Identifier: BSD-2-Clause |
| 7 |
# |
| 8 |
|
| 9 |
use strict; |
| 10 |
use warnings; |
| 11 |
use Getopt::Long; |
| 12 |
|
| 13 |
$| = 1; |
| 14 |
|
| 15 |
my (@yaml_files, $help, $mac_src, $mac_dst, $ip_src, $ip_dst, $inf); |
| 16 |
|
| 17 |
sub print_usage |
| 18 |
{ |
| 19 |
select(STDERR); |
| 20 |
|
| 21 |
print <<'EOF'; |
| 22 |
usage: prepare_config.pl [options] -- <yaml_files> |
| 23 |
|
| 24 |
options: |
| 25 |
--help, -h | Show this help |
| 26 |
--mac_src, -a | Source MAC Address |
| 27 |
--mac_dst, -b | Destination MAC Address |
| 28 |
--ip_src, -c | Source IP Address |
| 29 |
--ip_dst, -d | Destination IP Address |
| 30 |
--inf, -i | Network interface name |
| 31 |
EOF |
| 32 |
|
| 33 |
exit 0; |
| 34 |
} |
| 35 |
|
| 36 |
sub get_args |
| 37 |
{ |
| 38 |
GetOptions("help" => \$help, |
| 39 |
"a|mac_src=s" => \$mac_src, |
| 40 |
"b|mac_dst=s" => \$mac_dst, |
| 41 |
"c|ip_src=s" => \$ip_src, |
| 42 |
"d|ip_dst=s" => \$ip_dst, |
| 43 |
"i|inf=s" => \$inf) || print_usage(); |
| 44 |
|
| 45 |
@yaml_files = @ARGV; |
| 46 |
print_usage() unless @yaml_files; |
| 47 |
} |
| 48 |
|
| 49 |
sub process_yaml_file |
| 50 |
{ |
| 51 |
my ($file) = @_; |
| 52 |
my (@lines, $fh, $line, $mirror, $l2_dst, $l3_src, $l3_dst); |
| 53 |
|
| 54 |
$mirror = $file =~ /mirror/; |
| 55 |
|
| 56 |
$l2_dst = $mirror ? $mac_src : $mac_dst; |
| 57 |
$l3_dst = $mirror ? $ip_src : $ip_dst; |
| 58 |
$l3_src = $mirror ? $ip_dst : $ip_src; |
| 59 |
|
| 60 |
open $fh, '<', $file or die; |
| 61 |
|
| 62 |
while ($line = <$fh>) { |
| 63 |
$line =~ s/(TsnHigh|TsnLow|Rtc|Rta|Dcp|Lldp|UdpHigh|UpdLow|GenericL2)Interface: \w+/$1Interface: $inf/g |
| 64 |
if $inf; |
| 65 |
|
| 66 |
$line =~ s/(TsnHigh|TsnLow|Rtc|Rta|Dcp|Lldp|GenericL2)Destination: [0-9a-fA-F:]+/$1Destination: $l2_dst/g |
| 67 |
if $l2_dst; |
| 68 |
|
| 69 |
$line =~ s/(UdpHigh|UpdLow)Destination: \w+/$1Destination: $l3_dst/g |
| 70 |
if $l3_dst; |
| 71 |
|
| 72 |
$line =~ s/(UdpHigh|UpdLow)Source: \w+/$1Source: $l3_src/g |
| 73 |
if $l3_src; |
| 74 |
|
| 75 |
push @lines, $line; |
| 76 |
} |
| 77 |
|
| 78 |
close $fh; |
| 79 |
|
| 80 |
open $fh, '>', $file or die; |
| 81 |
print $fh $_ for @lines; |
| 82 |
close $fh; |
| 83 |
} |
| 84 |
|
| 85 |
get_args; |
| 86 |
|
| 87 |
process_yaml_file $_ for @yaml_files; |
| 88 |
|
| 89 |
exit 0 |