Skip to content

Commit 345d2e6

Browse files
committed
Update: Adding ability to active or deactivate firewall and show all open ports firewall
1 - Adding ability to active or deactivate firewall 2 - Adding ability to show open ports firewall 3 - Update read me file
1 parent c486743 commit 345d2e6

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

mahimana.sh

+65
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,63 @@ addSSHKey() {
531531
printf "${Green} 🎉 SSH key is added ${NC} \n";
532532
}
533533

534+
# Show Open ports
535+
showOpenPorts() {
536+
if command -v ufw &> /dev/null; then
537+
message=$(ufw status | grep -q "Status: active" && echo "ufw is enabled" || echo "ufw is disabled");
538+
# Check if message is "ufw is enabled" then show open ports
539+
if [ "$message" == "ufw is enabled" ]; then
540+
# Get all open ports and concat together with - ==> | tr '\n' '-' | sed 's/-$//'
541+
openPorts=$(ufw status | grep "ALLOW" | awk '{print $1}')
542+
printf "${Purple}Open ports:\n$openPorts\n ${NC}";
543+
else
544+
echo "$message";
545+
fi
546+
else
547+
echo "ufw is not installed"
548+
fi
549+
# Ask for back to main
550+
read -p "Do you want to back to main menu? (y/n): " -n 1 -r
551+
echo # (optional) move to a new line
552+
if [[ $REPLY =~ ^[Yy]$ ]]
553+
then
554+
main
555+
fi
556+
}
557+
558+
# Check Firewall
559+
CheckFirewall() {
560+
if command -v ufw &> /dev/null; then
561+
message=$(ufw status | grep -q "Status: active" && echo "ufw is enabled" || echo "ufw is disabled");
562+
echo "$message";
563+
else
564+
echo "ufw is not installed"
565+
fi
566+
}
567+
568+
# Change Firewall status
569+
ChangeFirewallStatus() {
570+
if command -v ufw &> /dev/null; then
571+
# Check if ufw is disabled the enable it and if it is enabled then disable it
572+
if ufw status | grep "Status: inactive"; then
573+
read -p "Enter the ports that you want to open: (separated by comma)" port;
574+
for p in $(echo $port | sed "s/,/ /g"); do
575+
ufw allow $p > /dev/null 2>&1;
576+
done
577+
ufw enable;
578+
else
579+
ufw disable;
580+
fi
581+
printf "${Green} 🎉 Firewall is changed ${NC} \n";
582+
CheckFirewall
583+
else
584+
echo "ufw is not installed";
585+
fi
586+
# Sleep 5sec
587+
sleep 5;
588+
main;
589+
}
590+
534591
# Main
535592
main() {
536593
clear
@@ -550,6 +607,8 @@ main() {
550607
printf "${Cyan}9. Get SSL for domain with Nginx ${Red}[Server]${NC}\n"
551608
printf "${Cyan}10. Install NVM (Node Version Manager)${NC}\n"
552609
printf "${Cyan}11. Add SSH Key ${Red}[Server]${NC}\n"
610+
printf "${Cyan}12. Active or deactive Firewall ${Purple}($(CheckFirewall))${NC}\n"
611+
printf "${Cyan}13. Show all Firewall open ports${NC}\n"
553612

554613
read -p "Enter your choice: " choice
555614

@@ -587,6 +646,12 @@ main() {
587646
11)
588647
addSSHKey
589648
;;
649+
12)
650+
ChangeFirewallStatus
651+
;;
652+
13)
653+
showOpenPorts
654+
;;
590655
*)
591656
printf "${Red}Invalid choice. Exiting.${NC}\n"
592657
exit 1

readme.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
" alt="shields" /></p>
99

1010
<h2>Screen Shot</h2>
11-
<img src="https://s8.uupload.ir/files/screenshot_from_2024-03-12_22-17-49_9pbz.png" alt="screenshot" />
11+
<img src="https://s8.uupload.ir/files/screenshot_from_2024-03-13_12-12-19_aglp.png" alt="screenshot" />
1212

1313
<h2>🧐 Features</h2>
1414

@@ -27,6 +27,8 @@ Here're some of the project's best features:
2727
* Get SSL with domain and Nginx
2828
* Install NVM (Node Version Manager)
2929
* Add SSH key
30+
* Active or deactivate firewall
31+
* Show all open ports firewall
3032

3133
<h2>🛠️ Usage</h2>
3234

0 commit comments

Comments
 (0)