Firewall Check Script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Firewall Check Script
# 1  
Old 03-17-2010
Error Firewall Check Script

Hello,

I made a following script that check every 5 minutes to check firewall is running or not, if firewall down that raise an alert only once, but following script generate an alert every 5 minutes according to cronjob:

Code:
FILE="/var/log/fwstatus" 
CHK="/tmp/fwstatus" 
 
service iptables status | if grep ESTABLISHED 1> /dev/null 2> /dev/null 
 
then 
rm -f $CHK 
echo "Firewall Running on Server" 
 
if test -f $CHK 
then 
echo "Not Sending an Email" 
exit 
fi 
 
 
else 
 
echo `date` | tee $FILE 
echo | tee -a $FILE 
 
echo "Firewall not Running" | tee -a $FILE 
echo | tee -a $FILE 
echo "Starting a Firewall Service" | tee -a $FILE 
echo | tee -a $FILE 
 
/scripts/fw-scripts/fw | tee -a $FILE (This is IPTABLES Script) 
 
if test $? = 0 
then 
echo "FW Run" | tee -a $FILE 
else 
echo "FW Stop" | tee -a $FILE 
fi 
 
echo "Now sending mail" 
$MAILPROG -s "$SUBJECT" "$EMAIL" < $FILE 
 
echo "Creating Tmp File" 
touch $CHK 
 
fi


Last edited by pludi; 03-17-2010 at 03:55 AM.. Reason: code tags, please...
# 2  
Old 03-25-2010
If I am understanding your issue correctly, you are getting an email alert that the firewall is down each time cron runs the job (every five minutes).

This reason you are getting an email each time is due to 2 factors:
1. Firewall is still down, even though your script is suppose to start it
2. You never check to see if $CHK, your /tmp/fwstatus file, exist before sending the email.

Change this:
Code:
        echo "Now sending mail"
        $MAILPROG -s "$SUBJECT" "$EMAIL" < $FILE
        echo "Creating Tmp File"
        touch $CHK
fi

To this:
Code:
    if test -f $CHK
                then
                # do nothing - it's existence means you sent an email already
                #   and hopefully that email made it to you :)
             else
                echo "Now sending mail"
                $MAILPROG -s "$SUBJECT" "$EMAIL" < $FILE
                echo "Creating Tmp File"
                touch $CHK
fi

Also, I noticed this and don't believe these lines are required - you remove $CHK, and then do a test to see if it's there.

Code:
service iptables status | if grep ESTABLISHED 1> /dev/null 2> /dev/null
        then
        rm -f $CHK
        echo "Firewall Running on Server"
line not needed ---->   if test -f $CHK
line not needed ---->           then
                        echo "Not Sending an Email"
                        exit
line not needed ---->   fi

Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to check the IP:PORT firewall uses?

I have my firewall process running # ps -ef | grep firewall root 21169 1 0 08:50 ? 00:00:00 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid I wish to know what ip : port number it is using. Can you please tell me how can i find out ? I tried the below command... (4 Replies)
Discussion started by: mohtashims
4 Replies

2. Shell Programming and Scripting

Good way to check firewall port on Linux centos 7

Hi, I need to know what kind of firewall settings does the linux box have? Is port 25 blocked in any way? Linux techx 3.10.0-514.10.2.el7.x86_64 #1 SMP Fri Mar 3 00:04:05 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux I'm coming from this thread. (1 Reply)
Discussion started by: mohtashims
1 Replies

3. Shell Programming and Scripting

SFTP script using firewall information

Hi, i have a scripts which transfer a file from source to dest server. It uses the firewall information like IP, USERNAME, PASSWORD. I wanted to know it belongs to source system firewall or the destination firewall info. How to check that firewall connection is working or no without running... (1 Reply)
Discussion started by: Pranavi
1 Replies

4. Shell Programming and Scripting

Script for checking firewall connection

Dear all I am writing a shell script to use telnet for the connection test There are 3 cases to test and detail as: /* Case 1 - The port can be connected */ # telnet host_a 20101 < /dev/null 2>&1 | grep -q Connected # echo $? return 0 /* Case 2 - The port cannot be connected */ #... (1 Reply)
Discussion started by: on9west
1 Replies

5. Debian

not run script firewall (lenny 5.0.4)

startup script displays a message: # /etc/init.d/firewall start Starting firewall: iptables iptables v1.4.2: Can't use -i with OUTPUT Try `iptables -h' or 'iptables --help' for more information. iptables v1.4.2: Can't use -i with OUTPUT not to understand what is wrong in the... (2 Replies)
Discussion started by: moskovets
2 Replies

6. Shell Programming and Scripting

Configure Firewall in unix from shell script

plz help me, i need to configure my firewall with using shell script, i am using unix fedora 9. thanks ppl. replys would be great. (1 Reply)
Discussion started by: king_jon85
1 Replies

7. Cybersecurity

The Best Script For Iptables Firewall

UTIN Firewall script for Linux 2.4.x and iptables ============================================== #!/bin/sh # # rc.firewall - UTIN Firewall script for Linux 2.4.x and iptables # # Copyright (C) 2001 Oskar Andreasson <bluefluxATkoffeinDOTnet> # # This program is free software; you can... (5 Replies)
Discussion started by: binhnx2000
5 Replies
Login or Register to Ask a Question