Perl 中的结构化模板

发布于 2024-10-20 12:03:32 字数 372 浏览 1 评论 0原文

我一直在 perl 中开发 nmap 解析器,我一直使用它,但是我想以非常结构化的方式写入文件。有问题的文件将是 nagios ports.cfg(我会在他们的 wiki 上使用 nmap2nagios.pl,但它似乎不起作用,而且我自己编写自己的文件很有趣)。

文件条目应如下所示

define hosts {
       host_name      $host_name || $host_ip
       alias          $os
       address        $host_ip
       }

上面只是一个示例,但我希望您能明白。任何帮助将不胜感激!我四处寻找一些模板模块,但它们似乎都非常适合 cgi。

I've been working on an nmap parser in perl, I've been going well with it, however I would like to write to a write to a file in a very structured manner. The file in question would be a nagios hosts.cfg (I'd use the nmap2nagios.pl on their wiki but it doesn't seem to be working and i'm having too much fun writing my own).

The file entries should look like this

define hosts {
       host_name      $host_name || $host_ip
       alias          $os
       address        $host_ip
       }

The above is just an example but I hope you get the idea. Any help will be appreciated! I've looked around for some template modules but they all seemed to be very geared towards cgi's.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浅听莫相离 2024-10-27 12:03:32

我赞同尼克的建议。一些灵感:

use Template;

my $tt = Template->new();
$tt->process( \*DATA,
    {   ip    => '10.90.0.1',
        host  => 'some',
        alias => 'some_alias',
    } => 'file.cfg'
) or die $tt->error();

__DATA__
define hosts {
       host_name      [% host %]
       alias          [% alias %]
       address        [% ip %]
       }

注意:模板引用模板工具包

I second Nick's suggestion. Some inspiration:

use Template;

my $tt = Template->new();
$tt->process( \*DATA,
    {   ip    => '10.90.0.1',
        host  => 'some',
        alias => 'some_alias',
    } => 'file.cfg'
) or die $tt->error();

__DATA__
define hosts {
       host_name      [% host %]
       alias          [% alias %]
       address        [% ip %]
       }

Note: The Template refers to Template Toolkit

满栀 2024-10-27 12:03:32

我总是使用 Template Toolkit 来输出任何结构化文档。它并不是专门针对 CGI - 例如,我很高兴地使用它来编写配置文件。

I would always use Template Toolkit for outputting any structured document. It's not specifically geared to CGI - I've used it happily to write config files for example.

温柔女人霸气范 2024-10-27 12:03:32

也许您正在寻找格式。我从未使用过它,语法看起来很疯狂,但你不需要任何模块。

Maybe you are looking for formats. I never used it and the syntax looks insane but you don't need any modules.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文