LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 02-17-2006, 07:15 PM   #1
abefroman
Senior Member
 
Registered: Feb 2004
Location: lost+found
Distribution: CentOS
Posts: 1,430

Rep: Reputation: 55
Need a shell scipt to get 2 variables per line and then run a command var 1 is over 5


I need a shell scipt to get 2 variables per line and then run a command var 1 is over 5

This will be in a cronjob run every 10 minutes

# netstat -an | grep FIN_WAIT2 | awk {'print $5'} | egrep -o -e "[0-9]{2,3}\.[0-9]{2,3}\.[0-9]{2,3}\.[0-9]{2,3}" | sort -n | uniq -c

1 59.16.157.234
1 68.146.16.24
103 66.41.61.85
113 67.138.240.10
5 67.15.188.37
1 67.15.191.12
155 67.15.203.30
19 67.18.113.196
1 81.208.34.83
1 83.50.170.92
1 84.103.227.56

What programming language should I use?

My guess is:
#!/bin/bash
for i in `netstat -an | grep FIN_WAIT2 | awk {'print $5'} | egrep -o -e "[0-9]{2,3}\.[0-9]{2,3}\.[0-9]{2,3}\.[0-9]{2,3}" | sort -n | uniq -c`;
do
$a = `$i | awk {'print $1'}`;
$b = `$i | awk {'print $2'}`;
if($a > 5)
then `apf -d $b`;
#I use the apf firewall and the command to block it is apf -d ip
done;

Is that the best way to do it?

If so what would the correct syntax be?
 
Old 02-18-2006, 12:11 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Use flag "-t" with netstat to narrow scope to TCP only (slightly faster), make sure to escape curly brakes with grep. For the rest it looks OK:

#!/bin/sh
netstat -ant | grep FIN_WAIT2 | awk {'print $5'} | grep -o -e "[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}" | \sort -n | uniq -c | while read count ip; do [ "$count" -gt "5" ] && apf -d $ip; done; exit 0

* A slightly "better" answer would be to tune your TCP stack values and application behaviour first and use Iptables limiting (hash or recent) if you have probs. Only then, when nasties keep ocurring, you should IMHO resort to "kludges" like this.
 
Old 02-18-2006, 03:46 PM   #3
abefroman
Senior Member
 
Registered: Feb 2004
Location: lost+found
Distribution: CentOS
Posts: 1,430

Original Poster
Rep: Reputation: 55
Thanks man!
 
  


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
my database export shell script run from command line but not form crontab saifee Linux - General 11 01-29-2012 09:24 AM
Using Variables in a Command that uses a Directory (Shell Script) CrimsonSkyZS Linux - General 4 01-11-2006 03:18 PM
how to include a shell scipt or call it at startup abhis_mail2002 Fedora 6 12-12-2005 11:30 PM
bash shell command line expansion hansi umayangan Linux - General 2 03-13-2005 11:31 AM
number of command line arguments to shell u4u Linux - General 1 03-04-2005 06:09 PM

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

All times are GMT -5. The time now is 04:32 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