希望你能帮助...
我有一个脚本,可用于根据所选功能创建防火墙规则。我想知道的是可以从命令行调用一个函数
EG ./script.sh 网页
#!/usr/bin/env bash
set -e
### Set IP ###
## Set Ports ###
CHAT=3000
HTTP=80
HTTPS=443
DNS=53
SSH=22
SMTP=25
MONGO=27017
NFS=111
GMAIL=587
### check for Root user ###
if [ "$(whoami)" == "root" ] ; then
echo "you are root"
else
echo "you are not root, This script must be run as root"
exit 1
fi
### Error Checking ###
error_check() {
if [ $? -eq 0 ]; then
echo "$(tput setaf 2) [ OK ] $(tput sgr0)"
sleep 2
else
echo "$(tput setaf 1) [ FAILED ] $(tput sgr0)"
exit 1
fi
}
### No command line arguments ###
if [[ $# -eq 0 ]] ; then
echo -e "\nUsage: $0 web nfs mail \n"
exit 0
fi
### Check which firewall is running ###
firewalld=`systemctl list-unit-files | grep firewalld | awk {'print $2'}`
iptables=`systemctl list-unit-files | grep iptables | awk {'print $2'}`
if [[ ${firewalld} == "enabled" ]]; then
echo "FirewallD is enabled.."
else
echo "Checking if iptables is enabled.."
if [[ ${iptables} == "enabled" ]]; then
echo "iptables is enabled.....Disabling"
systemctl stop iptables
systemctl disable iptables
echo "Starting FirewallD"
systemctl enable firewalld
systemctl start firewalld
echo "FirewallD is now enabled"
fi
fi
web() {
firewall-cmd --zone=public --add-port=${HTTP} --permanent
firewall-cmd --zone=public --add-port=${HTTPS} --permanent
sudo firewall-cmd --reload
}
这不是完成的文章,而是一个远景,但任何克服这个障碍的帮助都会很棒。
感谢:D