-1

这是我的配置,我正在尝试将其整合到一个可以粘贴到 Cisco ASA 中的路由命令中。

set device "internal"
set dst 13.13.13.13 255.255.255.255
set gateway 172.16.1.1

set device "internal"
set dst 14.14.14.14 255.255.255.255
set gateway 172.16.1.1

set device "internal"
set dst 15.15.15.15 255.255.255.255
set gateway 172.16.1.1

将其连接到单个设备,然后相应地对其进行修改以最终看起来像这样

route internal 13.13.13.13 255.255.255.255 172.161.1.1

然后其他3行可以消失。

我想在 Perl 中执行此操作,因为我正在编写其他脚本来执行源配置的其他部分

4

3 回答 3

0

假设您的订单始终为device, dstgateway则此方法有效:

use strict;
use warnings;


my @devices=(); #Array to store device strings
my $current_device=""; #String to capture current device.

while(<DATA>)
{
    chomp;
    if(/^set (\w+) (.+)$/)
    {
        if($1 eq "device")
        {
            $current_device="route $2";
            $current_device=~s/"//g;
        }
        elsif($1 eq "dst")
        {
            $current_device.=" $2";
        }
        elsif($1 eq "gateway")
        {
            $current_device.=" $2";
            push @devices,$current_device;
        }
    }
}

print "$_\n" foreach(@devices);




__DATA__
set device "internal"
set dst 13.13.13.13 255.255.255.255
set gateway 172.16.1.1

set device "internal"
set dst 14.14.14.14 255.255.255.255
set gateway 172.16.1.1

set device "internal"
set dst 15.15.15.15 255.255.255.255
set gateway 172.16.1.1

输出是:

route internal 13.13.13.13 255.255.255.255 172.16.1.1
route internal 14.14.14.14 255.255.255.255 172.16.1.1
route internal 15.15.15.15 255.255.255.255 172.16.1.1
于 2012-03-07T21:59:27.837 回答
0

如果您的配置文件包含由空行分隔的“段落”(节),如您的输入所示,您可以这样做:

#!/usr/bin/env perl
use strict;
use warnings;
local $/ = ""; #...paragraph mode...
while (<>) {
    chomp;
    m{^set.+"internal".+dst\s*([\d\.\s]+$).+gateway\s*(.+)$}ms and
        print "route internal $1 $2\n";
}
于 2012-03-07T22:17:49.347 回答
0

快速破解。这将读入一组值(由两个连续的换行符分隔),将其放入哈希中,然后将该哈希推送到数组中。使用输入记录分隔符$/读取记录。设置$/""空字符串有点像将其设置为"\n\n",请在上面链接的文档中阅读更多内容。

不太清楚你在这里需要什么。如果它只有一行合并,只需存储哈希而不将其推送到数组。它会“记住”第一条记录。

use strict;
use warnings;

$/ = "";   # paragraph mode
my @sets;
while (<DATA>) {
    my %set;
    chomp;               # this actually removes "\n\n" -- the value of $/
    for (split /\n/) {   # split each record into lines
        my ($set,$what,$value) = split ' ', $_, 3;  # max 3 fields
        $set{$what} //= $value;        # //= means do not overwrite
    }
    $set{device} =~ s/^"|"$//g;        # remove quotes
    push @sets, \%set;
}

for my $set (@sets) {  # each $set is a hash ref
    my $string = join " ", "route", 
        @{$set}{"device","dst","gateway"};  # using hash slice
    print "$string\n";
}

__DATA__
set device "internal"
set dst 13.13.13.13 255.255.255.255
set gateway 172.16.1.1

set device "internal"
set dst 14.14.14.14 255.255.255.255
set gateway 172.16.1.1

set device "internal"
set dst 15.15.15.15 255.255.255.255
set gateway 172.16.1.1

输出:

route internal 13.13.13.13 255.255.255.255 172.16.1.1
route internal 14.14.14.14 255.255.255.255 172.16.1.1
route internal 15.15.15.15 255.255.255.255 172.16.1.1
于 2012-03-08T02:28:46.853 回答