飞塔防火墙批量配置IP地址及IP组方法

使用过飞塔(FortiGate)防火墙的运维人员应该知道,它没有批量处理功能!!!

本文讲述如何编写Shell脚本远程批量添加IP地址,并将IP地址加入IP组。

主要方法:将所有ip地址保存到/opt/ip目录的任意txt文件中,系统定时执行如下脚本即可生成批量添加IP地址的命令行。

# IP列表文件
iplst="/opt/ip/*.txt"
# 只检测规定时间内更新的IP列表文件
ltime="-600"
# 飞塔防火墙信息
fwname=FG3950B-11111
fwaddr=10.10.200.201
fwgpnm=blacklist
usname=myname
passwd=mypswd
# 获取ip地址
find ${iplst} -amin ${ltime} | xargs awk '{print}' >> first.ip
[ ! -s first.ip ] && exit
# 格式化IP
sed -i 's/\ //g' first.ip
# 验证IP是否合法
egrep -o "((25[0-5]\.)|(2[0-4][0-9]\.)|(1[0-9][0-9]\.)|([1-9][0-9]\.)|([1-9]\.))((25[0-5]\.)|(2[0-4][0-9]\.)|(1[0-9][0-9]\.)|([1-9][0-9]\.)|([0-9]\.)){2}((25[0-5])|(2[0-4][0-9])|(1[0-9][0-9])|([1-9][0-9])|([1-9]))" first.ip > twice.ip
# 去重
sort -u twice.ip > all.ip
# 输出防火墙可用的格式
cat all.ip | while read ip
do
  echo $ip $ip/32 >> source.ip
done
# 连接防火墙并配置IP
cat source.ip | while read id ip
do
  /usr/bin/expect <<-EOF
  set timeout 5
  log_user 1
  spawn ssh ${myname}@${fwaddr}
  expect {
    "yes/no" {send "yes\r"; exp_continue}
    "password:" {send "${mypswd}\r"}
  }
  expect "${fwname} #" {send "config firewall address\r"}
  expect "(address) #" {send "edit $id\r"}
  expect "#" {send "set subnet $ip\r"}
  expect "#" {send "next\r"}
  expect "(address) #" {send "end\r"}
  expect "${fwname} #" {send "config firewall addrgrp\r"}
  expect "(addrgrp) #" {send "edit ${fwgpnm}\r"}
  expect "(${fwgpnm}) #" {send "append member $id\r"}
  expect "(${fwgpnm}) #" {send "next\r"}
  expect "(addrgrp) #" {send "end\r"}
  send "exit\r"
  expect eof
  EOF
done
# 删除临时文件
rm -f *.ip

将以上脚本加入定时任务即可。

该脚本需要expect命令,使用方法可参考文章Linux安装expect及用法实例

原创文章禁止转载:技术学堂 » 飞塔防火墙批量配置IP地址及IP组方法

精彩评论

7+2=

感谢您的支持与鼓励

支付宝扫一扫打赏

微信扫一扫打赏