#!/bin/bash # user ye #iptables指定ip访问指定端口,其他拒绝。 open_ip_group=("192.168.1.105" "192.168.1.106") port=$1 function config(){ local p=$1 iptables_num=$(iptables -L -n --line-numbers|grep "REJECT"|grep "${port}"|awk '{print $1}') if [ "$iptables_num" != "" ];then iptables -D INPUT $iptables_num fi for ip in ${open_ip_group[@]};do iptables -A INPUT -p tcp --dport ${p} -s ${ip} -j ACCEPT done iptables -A INPUT -p tcp --dport ${p} -j REJECT service iptables save iptables -L -n --line-numbers } function main(){ config $port } main