Linux Iptables:使用Shell脚本远程添加/删除IP地址
时间:2020-01-09 10:41:40 来源:igfitidea点击:
我有 root ssh访问,需要通过本地shell脚本使用IPtables命令动态添加/删除一些IP地址。
如何在CentOS/Redhat/RHEL/Debian/Ubuntu Linux下通过SSH会话远程添加或删除IP地址?
SSH客户端是用于登录到远程计算机并在远程计算机上执行命令的程序。
iptables命令用于在Linux内核中设置,维护和检查IP数据包过滤器规则表。
您可以使用Iptables命令本身添加或删除匹配规则。
您可以使用ssh客户端轻松添加或删除iptables规则
语法:添加IP地址
ssh [email protected] /sbin/iptables -I INPUT -i eth0 -s 1.2.3.4 -j ACCEPT ssh [email protected] /sbin/iptables -I INPUT -i eth0 -s 1.2.3.4 -d 192.54.1.2 -j ACCEPT ssh [email protected] /sbin/iptables -I INPUT -i eth0 -s 1.2.3.4 -d 192.54.1.2 -p tcp --destination-port 443 -j ACCEPT
语法:删除IP地址
ssh [email protected] /sbin/iptables -D INPUT -i eth0 -s 1.2.3.4 -j ACCEPT ssh [email protected] /sbin/iptables -D INPUT -i eth0 -s 1.2.3.4 -d 192.54.1.2 -j ACCEPT ssh [email protected] /sbin/iptables -D INPUT -i eth0 -s 1.2.3.4 -d 192.54.1.2 -p tcp --destination-port 443 -j ACCEPT
其中:
- -I INPUT将IP地址插入INPUT表。
- -i eth0:接口名称。
- -s 1.2.3.4:允许1.2.3.4 IP地址访问服务器。
- -d 192.54.1.2:服务器IP地址
- -p tcp destination-port 443:仅允许TCP端口443。
- -j ACCEPT操作设置为允许从1.2.3.4客户端IP到服务器IP 192.54.1.2的连接。
一个示例Shell脚本
#!/bin/bash
# A sample shell script to add or delete an IP over remove ssh session in bulk or a single IP at a time
# Written by , under GPL
# Usage:
# ./script.sh open "1.2.3.4:443:eth0:192.54.1.5:root:www03.example.com"
# ./script.sh close "1.2.3.4:443:eth0:192.54.1.5:root:www03.example.com"
# ------------------------------------------------------------------------
# note cut can be replaced with internal tring manipulation but, I prefer to use cut
# Purpose: add an IP over ssh
remoteipadd(){
local client=$(cut -d':' -f1<<<"")
local port=$(cut -d':' -f2<<<"")
local wallif=$(cut -d':' -f3<<<"")
local ip=$(cut -d':' -f4<<<"")
local vuser=$(cut -d':' -f5<<<"")
local vserver=$(cut -d':' -f6<<<"")
cmd="ssh ${vuser}@${vserver} /sbin/iptables -I INPUT -i ${wallif} -s ${client} -d ${ip} -p tcp --destination-port ${port} -j ACCEPT"
#echo "$cmd"
$cmd
}
# Purpose: Delete an IP over ssh
remoteipdelete(){
local client=$(cut -d':' -f1<<<"")
local port=$(cut -d':' -f2<<<"")
local wallif=$(cut -d':' -f3<<<"")
local ip=$(cut -d':' -f4<<<"")
local vuser=$(cut -d':' -f5<<<"")
local vserver=$(cut -d':' -f6<<<"")
cmd="ssh ${vuser}@${vserver} /sbin/iptables -D INPUT -i ${wallif} -s ${client} -d ${ip} -p tcp --destination-port ${port} -j ACCEPT"
#echo "$cmd"
$cmd
}
usage(){
echo "Usage: 1.2.3.4:443:eth0:192.54.11.5:root:vpn.example.com
1.2.3.5:443:eth0:192.54.3.5:root:mysql.example.com
1.2.3.65:22:eth1:192.54.2.5:root:www08.example.com
22.12.33.5:80:eth1:192.54.12.5:root:www04.example.com
{open|close} \"clientIP:serverPort:SeverInterface:serverIP:sshUser:sshServer\""
echo
echo -e "\twhile IFS= read -r line; do echo /path/to/script open "$line"; done <"/path/to/data.txt"
open \"1.2.3.4:443:eth0:192.54.1.5:root:www03.example.com\""
echo -e "\twhile IFS= read -r line; do echo /path/to/script close "$line"; done <"/path/to/data.txt"
close \"1.2.3.4:443:eth0:192.54.1.5:root:www03.example.com\""
echo
exit 1
}
line=""
[ $# -ne 2 ] && usage
case in
open) remoteipadd "$line";;
close) remoteipdelete "$line";;
*) usage
esac
您可以创建一个称为脚本的文本文件。
示例data.txt:
如下运行:
##代码##或者
##代码##
