我正在尝试自动为我的防火墙插入规则...使用Shell,但我是Shell编码的初学者...
#!/usr/bin/env bash
if [[ ! $EUID -eq 0 && ! $USER -eq "root" ]]; then
echo "You're not running as administator(root)!";
exit 1;
fi
declare -i allowRulesIDX=0;
declare -a allowRules;
$allowRulesIDX=$(($allowRulesIDX+1)); // Line number 10
$allowRules[$allowRulesIDX]=(80 "TLS/HTTP" "Local"); // Line number 11
$allowRulesIDX=$(($allowRulesIDX+1));
$allowRules[$allowRulesIDX]=(443 "SSL/HTTPS" "Local");
$allowRulesIDX=$(($allowRulesIDX+1));
$allowRules[$allowRulesIDX]=3306 "MySQL" "Local");
$allowRulesIDX=$(($allowRulesIDX+1));
$allowRules[$allowRulesIDX]=(53 "DNS" "Local");
$allowRulesIDX=$(($allowRulesIDX+1));
$allowRules[$allowRulesIDX]=(68 "DHCP" "Local");
$allowRulesIDX=$(($allowRulesIDX+1));
$allowRules[$allowRulesIDX]=(5353 "mDNS" "Local");
$allowRulesIDX=$(($allowRulesIDX+1));
$allowRules[$allowRulesIDX]=(853 "DNS" "Local");
$allowRulesIDX=$(($allowRulesIDX+1));
$allowRules[$allowRulesIDX]=(546 "DHCPv6" "Local");
$allowRulesIDX=$(($allowRulesIDX+1));
$allowRules[$allowRulesIDX]=(547 "DHCPv6" "Local");
$allowRulesIDX=$(($allowRulesIDX+1));
$allowRules[$allowRulesIDX]=(123 "NTP" "Local");
$allowRulesIDX=$(($allowRulesIDX+1));
$allowRules[$allowRulesIDX]=(5000 "UPnP" "Local");
declare -i rejectRulesIDX=0;
declare -a rejectRules;
$rejectRulesIDX=$((rejectRulesIDX+1));
$rejectRules[$rejectRulesIDX]=(5900 "VNC" "Anywhere");
declare -i RulesActionIDX=0;
declare -a RulesAction;
$RulesActionIDX=$((RulesActionIDX+1));
$RulesAction[$RulesActionIDX+1]=(${allowRules[@]} allow);
$RulesActionIDX=$((RulesActionIDX+1));
$RulesAction[$RulesActionIDX+1]=(${rejectRules[@]} reject);
for action in ${RulesAction[@]};
do
for rule in ${{action[0]}[@]};
do
for entry in ${rule[@]};
do
for $port in $(([[ ${entry[0]} =~ - ]] && {$((${entry[0]} | cut --fields=1 --delimiter=-))..$((${entry[0]} | cut --fields=2 --delimiter=-))} || ${entry[0]}));
do
for proto in udp tcp;
do
for direction in in out;
do
if [[ ${entry[2]} -eq "Anywhere" ]]; then
echo ufw ${action[1]} $direction log-all proto $proto from any to any port $port comment ${entry[1]};
elif [[ ${entry[2]} -eq "Local" ]]; then
for localTarget in 127.0.0.0/24 192.168.1.0/24;
do
echo ufw ${action[1]} $direction log-all proto $proto from $localTarget to any port $port comment ${entry[1]};
done;
else
echo ufw ${action[1]} $direction log-all proto $proto from ${entry[2]} to any port $port comment ${entry[1]};
fi
done;
done;
done;
done;
done;
done;
# Must always be the last line
exit 0;
- 请注意,我打印命令(而不是执行它们)是为了调试。
并且它导致:
line 10: 0=1: command not found
line 11: syntax error near unexpected token 80
我尝试了一些技巧来实现变量内的多维数组,但似乎我不能正确地实现它,变量的值不知何故被执行,而不是被增加?
1条答案
按热度按时间zsbz8rwp1#
我在这上面花了一些时间,因为我打算创建一个派生程序,将我定制的iptables编码/规则转换为nftables(* 指定的iptables替换 )。
我喜欢用*bash做一些事情,我非常努力让它只为bash工作,但这太令人沮丧了。
我放弃了bash,使用awk作为主要工具进行重写,如果有数千条规则,awk将是此类解析/转换的更好选择。
下面的脚本将执行我认为是您在上面提出的逻辑,但是由于我认为“ ${entry[0]} =~ - ”条件的语句格式不正确,因此产生了一些模糊性。我解释为此类情况是范围,不需要“拆分”。
此外,有时您会遇到逗号分隔的列表,您也不希望将其拆分,因此我修改了输入的格式,使用竖线(或管道符,“|“)作为字段分隔符。
您还会注意到,我再次修改了它,添加了accept/reject规范作为每个规范行的第一个字段(“class”**)。
我也不太明白您要对“Anywhere”或“Local”以外的目标值做些什么。如果可能,我建议您考虑定义规范时不要用双引号括住它们,以减少所涉及的解析和逻辑工作。
下面是生成的代码逻辑。我希望它对你有用。
会话日志如下: