我是 BASH 脚本的初学者,我正在尝试编写脚本来配置 iptables,但第一个块似乎出了问题,请指教
#!/bin/bash
echo "IPTABLE ASSIGNMEN"
echo -e "1.Reject all except one IP\n2.Flush iptables (*Use Carefully_it will remove all the rules from iptables*)\n"
echo -e "Select Number"
read number;
if [[$number == "1"]]; then
echo "Insert IP\n"
read ip;
iptables -A INPUT -s $ip -j ACCEPT
iptables -A OUTPUT -d $ip -j ACCEPT
iptables -P INPUT DROP
iptables -P OUTPUT DROP
fi
答案1
答案2
谢谢大家的支持,实际上正如你提到的,我也使用了case语句,也是由于错误$'\r': 未找到命令,这是由编辑器样式引起的,我使用了“dos2unix 文件名.sh“解决它并运行脚本”sudo bash 文件名.sh“ 其运行正常。
!/bin/bash
echo "IPTABLE ASSIGNMEN"
echo -e "1.Reject all except one IP\n2.Flush iptables (*Use Carefully_>
echo -e "Select Number\n"
read x;
case $x in
"1" )
echo "Insert IP\n"
read ip;
iptables -A INPUT -s $ip -j ACCEPT
iptables -A OUTPUT -d $ip -j ACCEPT
iptables -P INPUT DROP
iptables -P OUTPUT DROP
;;
"2" )
iptables -F
iptables -X
iptables -P INPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -P OUTPUT ACCEPT
;;
* )
echo "Try Again"
;;
esac