将多行连接到一个和黑客内容

时间:2012-03-07 21:26:17

标签: perl cisco

这是我的配置,我正在尝试将其合并为一个可以粘贴到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中执行此操作,因为我正在编写其他脚本来执行源配置的其他部分

3 个答案:

答案 0 :(得分:0)

假设您的订单始终为devicedstgateway,那么这可行:

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

答案 1 :(得分: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";
}

答案 2 :(得分: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