需要帮助通过导入 ip/mac 条目列表文件为批量 ipmacbinding 配置强化防火墙 CLI 代码

need help on configuring fortigate firewall CLI code for bulk ipmacbinding by importing a ip/mac entries list file

我在 Fortigate 200d 中遇到 ip/mac 绑定问题,问题是我有一个包含 3000 个 IP/MAC 地址条目的列表,我将它们保存在一个 csv 文件中。

这就是我要找的东西

1.I 想编写一个可以导入该文件的代码

2.I 想在循环中执行此代码片段,直到更新所有条目。

config firewall ipmacbinding table
edit <index_int>
    set ip <address_ipv4>
    set mac <address_hex>
    set name <name_str>
    set status {enable}
end
  1. 在上面的代码片段的帮助下,每次我必须手动输入 IP、MAC 和名称值 3000 次,而不是我只想导入一个文件和应添加该文件中的值。

  2. 在一些地方我知道它可以在 perl/python 脚本的帮助下实现,但我不知道。

我用谷歌搜索,但没有找到有关此信息的任何地方,所以我希望我能得到帮助来完成这项任务。

谢谢。

CSV 文件格式为

Index   IP       Mac                name
1   10.10.17.1  aa:bb:cc:00:11:22   first
2   10.10.17.2  cc:dd:ee:ff:22:33   second
3   10.10.17.3  33:44:11:3f:00:88   third

Formal of CSV File

我从未使用过 fortigate CLI,所以我假设您知道它是如何工作的以及如何使用它。下面是一个小尝试,如果它不能正常工作,希望能让您走上正确的道路。我假设当您 运行 配置命令时,终端通常会等待用户输入。所以在这种情况下,perl 脚本将输入该输入。

use strict;
use warnings;

my $csv_file = shift;
open (my $cfh, '<', $csv_file) or die "Unable to open $csv_file: $!";
my @headers = split (' ', <$cfh>);

while(<$cfh>){
  my %config;
  my @data = split(' ');
  @config{@headers}=@data;

  open(my $firewall, '|-', 'config firewall ipmacbinding table') or die "Unable to open 'config firewall ipmacbinding table': $!";
  print $firewall "edit ",$config{'Index'},"\n";
  print $firewall "set ip ",$config{'IP'},"\n";
  print $firewall "set mac ",$config{'Mac'},"\n";
  print $firewall "set name ",$config{'name'},"\n";
  print $firewall "set status {enable}\n";
  print $firewall "end\n";
  close $firewall;
}

以上内容旨在帮助您开始了解如何完成这项工作。正如我所说,我没有使用 fotigate 的经验,所以你可能需要稍微调整一下。

如果我选择仅将其打印到我的终端屏幕作为这样的输出

use strict;
use warnings;

my $csv_file = shift;
open (my $cfh, '<', $csv_file) or die "Unable to open $csv_file: $!";
my @headers = split (' ', <$cfh>);

while(<$cfh>){
        my %config;
        my @data = split(' ');
        @config{@headers}=@data;

        #open(my $firewall, '|-', 'config firewall ipmacbinding table') or die "Unable to open 'config firewall ipmacbinding table': $!";
        print  "config firewall ipmacbinding table\n";
        print  "\tedit ",$config{'Index'},"\n";
        print  "\tset ip ",$config{'IP'},"\n";
        print  "\tset mac ",$config{'Mac'},"\n";
        print  "\tset name ",$config{'name'},"\n";
        print  "\tset status {enable}\n";
        print  "end\n";
        #close $firewall;
}

它产生以下内容

config firewall ipmacbinding table
        edit 1
        set ip 10.10.17.1
        set mac aa:bb:cc:00:11:22
        set name first
        set status {enable}
end
config firewall ipmacbinding table
        edit 2
        set ip 10.10.17.2
        set mac cc:dd:ee:ff:22:33
        set name second
        set status {enable}
end
config firewall ipmacbinding table
        edit 3
        set ip 10.10.17.3
        set mac 33:44:11:3f:00:88
        set name third
        set status {enable}
end

希望这足以让您入门。