Compare lines and execute


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Compare lines and execute
# 1  
Old 07-07-2008
Compare lines and execute

Greetings,

I'm new to scripting and need a little help. I have a NAS server and want to make a script that bans IP if they fail login several times.

The problem is that I dont know how to compare the IP's in the file. If they exist more than 3 times do ....

#!/bin/sh
set -x

X=4
rm /home/banip/output
touch /home/banip/output.1

grep "Invalid user root" /var/log/messages|cut -c 68-79 > /home/banip/output.1

#If the same entry is gathered in the file more than x times

A=`cat /home/banip/ban_this_ip|wc -l`
echo $A
if [ "$A" -ge "$X" ]
then
cat $ theIP >> /etc/config/ipsec_deny.conf
else

echo "Nothing to worry about"

fi

#########################################
#Output.1 now contains:

218.22.9.118
218.22.9.118
218.22.9.118

So if the above scenario where the same IP exists more 3 times (is unique) then insert this ip into /etc/config/ipsec_deny.conf
# 2  
Old 07-07-2008
Something like this perhaps? (untested)
Code:
grep "Invalid user root" /var/log/messages|cut -c 68-79 | sort | uniq -c | awk '$1 > 3 { print $2 }' >> /etc/config/ipsec_deny.conf

I'd guess you'd want to make it a bit fancier though, to check whether the IP already exists, etc.
# 3  
Old 07-08-2008
Quote:
Originally Posted by Annihilannic
Something like this perhaps? (untested)
Code:
grep "Invalid user root" /var/log/messages|cut -c 68-79 | sort | uniq -c | awk '$1 > 3 { print $2 }' >> /etc/config/ipsec_deny.conf

I'd guess you'd want to make it a bit fancier though, to check whether the IP already exists, etc.
Hi Annihilannic,

I messed around yesterday and got this:

#!/bin/sh
set -x
B=1:
rm /home/banip/output.1
touch /home/banip/output.1

grep "Invalid user root" /var/log/messages|cut -c 68-79 > /home/banip/output.1

#If same entry is there more than 3 times

tmp1="`head -n 1 /home/banip/output.1`"
tmp2="`sed -n 2p /home/banip/output.1`"
tmp3="`sed -n 3p /home/banip/output.1`"

if [ $tmp1 == $tmp2 ] && [ $tmp1 == $tmp3 ]
then

A="`cat output.1| head -n 1`"

echo "$B""$A" >> /etc/config/ipsec_deny.conf
echo "$A is not banned"

I know its ugly and by far bulletproof - I will try to use your line and mess around. Always nice with inspiration Smilie

Thanks for your time much appreciated!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Compare lines between two files

I have two files I need to compare these two files and take the lines that are common in both the files and consider the line present in second file for my further processing I have used "Awk" along with "FNR and NR" but that is not working gawk -F= ' > FNR==NR {a=$1; next}; > ... (2 Replies)
Discussion started by: Priya Amaresh
2 Replies

2. UNIX for Dummies Questions & Answers

Compare lines in 2 files

I have 2 files with exactly the same information (with header and separated by ";") and what I would like to do is print (for both files!) the columns that are different and also print the "key" column that is equal in the 2 files For example, if File1: key1;aaa;bbb;ccc key2;ddd;eee;fff... (4 Replies)
Discussion started by: mvalonso
4 Replies

3. Shell Programming and Scripting

Count, compare, execute ...

I'm working on a script that needs to execute only after directory B is equal in file count to directory A. It needs to check dir B every 5 min or so and when the file count is equal to A, it executes a script. I'm thinking of using wc -w but I'm not sure how to put it together. Any help... (7 Replies)
Discussion started by: scribling
7 Replies

4. Shell Programming and Scripting

Compare values for a pattern match and execute script

Here in the input file 23:59:13,devicename,21,server1,700 23:59:13,devicename,22,server2,200 23:59:13,devicename,23,server3,200 23:59:13,devicename,24,server4,200 23:59:13,devicename,25,server5,200 23:59:13,devicename,26,server6,200 23:59:13,devicename,27,server7,200... (6 Replies)
Discussion started by: necro98
6 Replies

5. Shell Programming and Scripting

Compare lines within a file

could someone write a short script which is able to take a text file input and compare lines 1 and 2, 3 and 4, 5 and 6 ... ... until the end of the file. And to prompt if the lines are not equal. Thank you! (10 Replies)
Discussion started by: c_lady
10 Replies

6. Shell Programming and Scripting

Execute the Output Lines.

Hi, My Script looks like this #! /bin/ksh cd /usr/SrcFiles/ADD; while read line do d=`echo $line|cut -d'.' -f1` echo $d".XML" >> XML_File_List.txt out=`echo "gunzip -c -S .ZIP $d.ZIP > $d.XML;"` echo $out per=`echo "chmod ugo+rwx $d.XML;"` echo $per done <ZIP_File_List.txt and the... (5 Replies)
Discussion started by: naveen.kuppili
5 Replies

7. Shell Programming and Scripting

Execute output lines

I have a command (not shell script) that general several lines of output like this... scp /var/lib/mysql/jitu/email.* root@172.29.0.218:/root/pitu/ scp /var/lib/mysql/jitu/orders.* root@172.29.0.218:/root/pitu/ I tried adding xargs but it did not work. -exec was not tried because I guess it... (3 Replies)
Discussion started by: shantanuo
3 Replies

8. Shell Programming and Scripting

How to execute the rest of the code after commenting multiple lines?

Hi, As I have seen in this forum how to comment multiple lines in the script, but it does not work properly for me. It is blocking the code but it does not execute the rest of the codes. This is my code #! /usr/bin/ksh month='date +"m%"' : << Comments Block if || then echo "inc =... (12 Replies)
Discussion started by: Yamini Thoppen
12 Replies

9. Shell Programming and Scripting

Trying to compare lines in 2 files

Hello, I am new to scripting and need some help. In looking at other posts on this forum, I came up with the following logic. I cannot figure out why I am getting names of files of the current directory in my echo output. Scenario: message file has a line containing the version. Version.txt... (2 Replies)
Discussion started by: brdholman
2 Replies

10. Shell Programming and Scripting

How can I get an if statement to execute based on number of lines in a file?

I need to have an if statement in a script to run if there are certain processes running. Easiest way I can see to do this is to run a ps and grep the results based on what I am looking for: $ ps -ef | grep wtrs --- webtrend 5046 1 0 May 12 ? 0:28 /webtrends/versions/6.1/wtrs_ui... (6 Replies)
Discussion started by: LordJezo
6 Replies
Login or Register to Ask a Question