0

Hope you can help...

I have a script that will be used to create firewalld rules depending on the chosen function. What I would like to know is it it possible to call a function from the commandline

E.G ./script.sh web

#!/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       

}

This is not the finished article but a long shot, but any help getting over this hurdle would be great.

Thanks :D

CJW101
  • 1
  • 2
  • Solved it using case. `case "$1" in (web) web exit 1 ;; (nfs) nfs exit 0 ;; esac` – CJW101 Sep 21 '17 at 15:59
  • You can check this case very similar and well explained:https://unix.stackexchange.com/questions/47695/how-to-write-startup-script-for-systemd – Ardit Sep 21 '17 at 16:15
  • Thanks Ardit, that helps with an issue I have with node_exporter, thanks. – CJW101 Sep 24 '17 at 19:58

0 Answers0