how to run this script in cron.d every minute?

Vlark.Lopin

i am using ubuntu 15.04 i got huge ddos attacks i wanted to stop that attacks by blocking attackers ips so i am planing to run this script every minute inside cron.d and make it automatically start

how can i do that

#!/bin/bash

#Collecting list of ip addresses connected to port 20000

netstat -plan|grep :20000|awk {'print $5'}|cut -d: -f 1|sort|uniq -c|sort -nk 1 > /root/iplist

#Limit the no of connections
LIMIT=10;

for ip in `cat /root/iplist |awk '{print $2}'`;do

if [ `grep $ip /root/iplist | awk '{print $1}'` -gt $LIMIT ]
then
echo "100 connection from $ip... `grep $ip /root/iplist | awk '{print $1}'` number of connections... Blocking $ip";

#Blocking the ip ...

/etc/rc.d/init.d/iptables save > /dev/null;
CHECK_IF_LOCALIP=0;
/sbin/ifconfig | grep $ip > /dev/null;
if [ $? -ne $CHECK_IF_LOCALIP ]
then
{
FLAG=0;
grep $ip /etc/sysconfig/iptables | grep DROP > /dev/null;
if [ $? -ne $FLAG ]
then
iptables -I INPUT -s $ip -j DROP;
else
echo " Ipaddress $ip is already blocked ";
fi
}
else
echo " Sorry, the ip $ip cannot be blocked since this is a local ip of the server ";
fi
fi
done 
heemayl

Focusing on the cron entry rather than the content of the script, assuming the script is ~/foobar.sh, to run the script every minute as cron job, open your cron table by crontab -e and add:

*/1 * * * * ~/foobar.sh

Make sure the script is executable at first.

*/1 in the minute column will make crond to execute the script every minute, while if you put just 1 the script will be executed only on the first minute of the given hour(s).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to run cron in serverless for every minute?

How to run a cron job to execute every minute?

How to run a CRON every 8h and 1 minute

How to run a cron jobs in every minute for a specific hour on server

run cron every minute during a specific hour?

Run a cron job every minute, meaning of syntax

How do I make cron run something every "N"th minute, where n % 5 == 1?

How to set cron job to run every minute between 11 PM to 12AM Midnight

jenkins cron: run every 3rd minute, starting at 1

Run cron job every 2 hours In the X minute

Would running a certain script every minute via cron be detrimental to anything?

Run a script via cron every other week

How to run every 3 minute in Quartz scheduler?

How to fire a cron every minute for two separate applications?

Why does my cron job run every 1 minute although I set it every 1 hour?

Setting cron task every day at 2am.. makes it run every minute

Why my cron job is running every minute when it is scheduled to run every hour?

How do I run a script for 1st working day of every month in cron?

How to make cron run only 42 times in 10 minute intervals?

How to run a cron task at the last minute of each day?

Run cron job every 2 minutes except the first minute "0" of the first hour "0"

How to run an iOS application every 1 minute infinitely?

Android - how to run a piece of code every minute (synced with the device time)

how to schedule a c function to run exactly every minute

How to configure azure webjob to run every 15 minute

How do I make this function in Google sheet run "every minute"?

how to run `php artisan queue:work` every minute on cpanel?

How to create a spreadsheet script that moves to the next sheet tab every minute?

Run a function at the start of every minute?