從IP清單轉fortinet firewall語法

<?php

function gen_fw_config_mulit($IP){
  #10.28.101.135-155
  $tmp = preg_split('/\./', $IP);
  $ip_head = "{$tmp[0]}.{$tmp[1]}.{$tmp[2]}";
  $tmp2 = preg_split('/-/', $tmp[3]);
  $ip_start = $tmp2[0];
  $ip_end = $tmp2[1];

  #output config
  echo "edit \"$ip_head.[$ip_start-$ip_end]\"\n";
  echo "    set associated-interface \"P1\"\n";
  echo "    set type iprange\n";
  echo "    set end-ip $ip_head.$ip_end\n";
  echo "    set start-ip $ip_head.$ip_start\n";
  echo "next\n";
}

function gen_fw_config_single($IP){
  #10.28.101.135
  #output config
  echo "edit \"$IP/32\"\n";
  echo "    set associated-interface \"P1\"\n";
  echo "    set subnet $IP 255.255.255.255\n";
  echo "next\n";
}

?>

 

<?php

$ip_file = 'iplist.txt';

require_once 'config.inc.php';
$lines = file($ip_file);
foreach($lines as $line){
  $line = trim($line);
  if (stripos($line, '-') !== false){ //multi IP
    #echo "#$line\n";
    gen_fw_config_mulit($line);
  }else { //single IP
    gen_fw_config_single($line);
  }//if
}//foreach
?>

 

Facebook Comments Box

Leave a Reply