LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-24-2001, 06:38 PM   #1
tarballedtux
Member
 
Registered: Aug 2001
Location: Off the coast of Madadascar
Posts: 498

Rep: Reputation: 30
Question ipchains Bash script


I am having a problem with this script where it will not terminate
after the appropriate calculations take place.

#!/bin/bash
echo > stat_deny
grep DENY /var/log/messages | cut -d' ' -f12 | cut -d: -f1 > d_pack
cp d_pack d_pack2
da=`wc -l d_pack2 | cut -d'd' -f1`
left=`wc -l d_pack2 | cut -d'd' -f1`
until [ "$left" = 0 ]
do
left=`wc -l d_pack2 | cut -d'd' -f1`
s_ip=`head -n 1 d_pack2`
grep -v $s_ip d_pack2 > d_ex
n_word=`wc -l d_ex | cut -d'd' -f1`
diff=`expr $da - $n_word`
echo "$s_ip was blocked $diff time(s)." >> stat_deny
echo "$s_ip was blocked $diff time(s)."
cp d_ex d_pack2
echo > d_ex
da=`expr $da - $diff`
done


I am using RH7.0
Pentium 2 350Mhz
with bash-2.04-11
 
Old 08-24-2001, 09:28 PM   #2
tarballedtux
Member
 
Registered: Aug 2001
Location: Off the coast of Madadascar
Posts: 498

Original Poster
Rep: Reputation: 30
BashScript to create some stats from the logs

The first script i posted isn't the partional working model.
This one will do the calcualtions correctly but not terminate.

#Major Variables
#
# da --line count of d_pack
# s_ip --stopped ip address in log
# left --number of lines left in d_pack2
# diif --since I inverse grep a file I compare the line # counts between the before grep and after grep
# Files
# d_pack -- copy of /var/log/messages grepped for string # DENY
# d_pack2 -- actual file manipulated in script
# stat_deny -- the statitistics ogf the blocked ip addresses


#!/bin/bash
echo > stat_deny
grep DENY /var/log/messages | cut -d' ' -f12 | cut -d: -f1 > d_pack
cp d_pack d_pack2
da=`wc -l d_pack2 | cut -d'd' -f1`
function weedout()
{
s_ip=`head -n 1 d_pack2`
grep -v $s_ip d_pack2 > d_ex
n_word=`wc -l d_ex | cut -d'd' -f1`
diff=`expr $da - $n_word`
echo "$s_ip was blocked $diff time(s)." >> stat_deny
echo "$s_ip was blocked $diff time(s)."
cp d_ex d_pack2
echo > d_ex
da=`expr $da - $diff`
}
left=`wc -l d_pack2 | cut -d'd' -f1`
while [ "0" -lt $left ]
do
weedout
done
 
Old 08-25-2001, 12:03 AM   #3
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
Nice script.

The reason your script does not stop is because you never decrement you test variable $left. If you add that line to the end of your function then it works correctly.

I noticed that you most of your file names began with d_. I'm guess you did this because you needed to cut out the d_ from the output of wc? Is that correcet? You could also cut it out with the following line.
Code:
NUM=`wc -l | awk ' { print $1 } '
This way you can name your files whatever you wanted.

Noticing how long the script took to execute. ( I have a P100) I took a stab at a possibly faster way to do this. My method was about 10 seconds faster on my machine with my given data set; however, it seems to be more costly if there are many hits from one IP.

Code:
#!/bin/bash

grep DENY /var/log/messages.010407 | cut -d' ' -f12 | cut -d: -f1 > ip_list

sort ip_list > sorted_ip

echo > stat_deny
COUNT=1
LAST=0.0.0.0
for i in `cat sorted_ip`; do
   if [ $LAST = $i ]; then
      COUNT=`expr $COUNT + 1`
   else
      echo "$LAST was blocked $COUNT time(s)." | tee -a stat_deny
      COUNT=1
   fi
   LAST=$i
done
Gary
 
Old 08-25-2001, 10:55 AM   #4
tarballedtux
Member
 
Registered: Aug 2001
Location: Off the coast of Madadascar
Posts: 498

Original Poster
Rep: Reputation: 30
Thanks

Well for one thing the question about the file naming was right,
but actually the d_ stood for (deny) as in denied packets.

Your script works the same as mine but it still will not terminate.
I think something is wrong on my end, any ideas?

I never heard of the sort command before that would have saved my some time in writing this script.

If you could help with the termination problem I could finally finish the script.

Thanks for you help.
 
Old 08-25-2001, 09:57 PM   #5
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
Don't know why it will not terminate. Both scripts ended find on my box. Try limiting your data to just a few lines of DENY(s) from messages and execute the script in debug mode.

sh -x scriptname.sh

This will allow you to see the progression of all the variables, including your termination condition.


Gary
 
Old 08-26-2001, 07:14 AM   #6
tarballedtux
Member
 
Registered: Aug 2001
Location: Off the coast of Madadascar
Posts: 498

Original Poster
Rep: Reputation: 30
ipchians script

You were (write) your script worked fro me when i limited it to
< 200 DENY's. Apparently the script was hanging on an ip with 4444 DENY's (LOL)

Thnaks for oyur help.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
ipchains startup script (RH v.6.1) dbrooke Linux - Networking 3 12-19-2004 03:56 PM
send automatic input to a script called by another script in bash programming jorgecab Programming 2 04-01-2004 12:20 AM
bash script - incrementing a filename in a script tslinux Programming 10 08-05-2003 11:58 PM
Bash Script how to Senta Programming 4 06-05-2003 04:42 AM
bash script prob: how can i tell the script that a 'dd' has finished? Frustin Linux - General 2 04-02-2003 05:34 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:33 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration