LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-12-2009, 06:29 AM   #1
kotlt
LQ Newbie
 
Registered: Mar 2006
Posts: 11

Rep: Reputation: 0
Pleas help me to setup this crontab


I have three file

iptables.sh
Code:
IPT=/sbin/iptables
SPAMLIST=”spamlist”
SPAMDROPMSG=”SPAM LIST DROP”
BADIPS=$(egrep -v -E “^#|^$” /root/iptables/blocked.ips)

# create a new iptables list
$IPT -N $SPAMLIST

for ipblock in $BADIPS
do
$IPT -A $SPAMLIST -s $ipblock -j LOG –log-prefix “$SPAMDROPMSG”
$IPT -A $SPAMLIST -s $ipblock -j DROP
done

$IPT -I INPUT -j $SPAMLIST
$IPT -I OUTPUT -j $SPAMLIST
$IPT -I FORWARD -j $SPAMLIST
autoblock.sh
Code:
/etc/init.d/iptables start
cd /root/iptables

netstat -atun | grep SYN_RECV | awk ‘{print $5}’ | cut -d: -f1 |sort | uniq -d | sort -n > blocked.ips
sh ./iptables.sh
and blocked.ips.

I chmod all file and folder to 777.

When i run command to test
sh /root/iptables/autoblock.sh, it says

Quote:
[root@server ~]# sh /root/iptables/autoblock.sh
: command not found 2:
: command not found 4:
./iptables.sh: line 7: LIST: command not found
./iptables.sh: line 8: ^$: command not found
I'm using centos. Can anyone please correct this sciprts for me ?


And i unable to save crontab.

Code:
crontab -e
and type
Code:
*/2 * * * * /root/iptables/autoblock.sh
I don't know how to save, i tried :wq, :q, :q! , :w! , but nothing changes.

Please help me. Thank you
 
Old 10-12-2009, 06:49 AM   #2
rizhun
Member
 
Registered: Jun 2005
Location: England
Distribution: Ubuntu, SLES, AIX
Posts: 268

Rep: Reputation: 47
Hmmm...

The ':wq' should work as long as your $EDITOR is vi/vim.

I'd start by adding a proper sh-bang to the top of those scripts (so you don't need to prefix with the 'sh'):
Code:
#!/bin/bash
I'd also redirect the output on your cron entry:
Code:
*/2 * * * * /root/iptables/autoblock.sh > /dev/null 2>&1
Do you get an error when you try to write & quit?

See if that helps.
 
Old 10-12-2009, 06:50 AM   #3
linuxlover.chaitanya
Senior Member
 
Registered: Apr 2008
Location: Gurgaon, India
Distribution: Cent OS 6/7
Posts: 4,631

Rep: Reputation: Disabled
There is some issue with your script. It is giving you errors. In your first script I do not see ipblock variable declared.
Also if your default editor is vi then you should use Esc + :wq to save and quit.
Esc to go from insert mode to command mode.

Oh yes, and if you forgot, insert the shebang line at the top of the script. rizhun has given that already.

Last edited by linuxlover.chaitanya; 10-12-2009 at 06:52 AM.
 
Old 10-12-2009, 07:03 AM   #4
kotlt
LQ Newbie
 
Registered: Mar 2006
Posts: 11

Original Poster
Rep: Reputation: 0
Thank you, i have create crontab sucessful. Anyone please correct my code ? I still get error



: command not found 2:
: command not found 4:
./iptables.sh: line 7: LIST: command not found
./iptables.sh: line 8: ^$: command not found
 
Old 10-12-2009, 07:22 AM   #5
linuxlover.chaitanya
Senior Member
 
Registered: Apr 2008
Location: Gurgaon, India
Distribution: Cent OS 6/7
Posts: 4,631

Rep: Reputation: Disabled
Do you have the shebang line at the top of the script? Forget about the ipblock comment in my previous post I missed it.
 
Old 10-12-2009, 07:28 AM   #6
kotlt
LQ Newbie
 
Registered: Mar 2006
Posts: 11

Original Poster
Rep: Reputation: 0
Yes i added it, and still got error
 
Old 10-12-2009, 07:31 AM   #7
repo
LQ 5k Club
 
Registered: May 2001
Location: Belgium
Distribution: Arch
Posts: 8,529

Rep: Reputation: 899Reputation: 899Reputation: 899Reputation: 899Reputation: 899Reputation: 899Reputation: 899
Try
Code:
netstat -atun | grep SYN_RECV | awk '{print $5}' | cut -d: -f1 |sort | uniq -d | sort -n > blocked.ips
do a
Quote:
cat blocked.ips
to see the content

Last edited by repo; 10-12-2009 at 07:32 AM.
 
Old 10-12-2009, 07:37 AM   #8
kotlt
LQ Newbie
 
Registered: Mar 2006
Posts: 11

Original Poster
Rep: Reputation: 0
Still got message, repo

Code:
./iptables.sh: line 4: LIST: command not found
./iptables.sh: line 5: ^$: command not found
My current iptables.sh

Code:
#!/bin/bash
IPT=/sbin/iptables
    SPAMLIST=”spamlist”
    SPAMDROPMSG=”SPAM LIST DROP”
    BADIPS=$(egrep -v -E “^#|^$” /root/iptables/blocked.ips)
    $IPT -N $SPAMLIST
   for ipblock in $BADIPS
    do
    $IPT -A $SPAMLIST -s $ipblock -j LOG –log-prefix “$SPAMDROPMSG”
    $IPT -A $SPAMLIST -s $ipblock -j DROP
    done
    $IPT -I INPUT -j $SPAMLIST
    $IPT -I OUTPUT -j $SPAMLIST
    $IPT -I FORWARD -j $SPAMLIST

//remove LIST in line #4 and still got error
Code:
./iptables.sh: line 5: ^$: command not found

Last edited by kotlt; 10-12-2009 at 07:42 AM.
 
Old 10-12-2009, 07:45 AM   #9
linuxlover.chaitanya
Senior Member
 
Registered: Apr 2008
Location: Gurgaon, India
Distribution: Cent OS 6/7
Posts: 4,631

Rep: Reputation: Disabled
Code:
egrep -v -E “^#|^$”
Can you change this to

egrep -v -E ^#|^$
 
Old 10-12-2009, 07:53 AM   #10
rizhun
Member
 
Registered: Jun 2005
Location: England
Distribution: Ubuntu, SLES, AIX
Posts: 268

Rep: Reputation: 47
Try changing:

SPAMDROPMSG=”SPAM LIST DROP”

to:

SPAMDROPMSG='SPAM LIST DROP'
 
Old 10-12-2009, 07:53 AM   #11
kotlt
LQ Newbie
 
Registered: Mar 2006
Posts: 11

Original Poster
Rep: Reputation: 0
I replaced, but still got same error

Code:
[root@server ~]# sh /root/iptables/autoblock.sh
./iptables.sh: line 5: ^$: command not found
 
Old 10-12-2009, 07:56 AM   #12
repo
LQ 5k Club
 
Registered: May 2001
Location: Belgium
Distribution: Arch
Posts: 8,529

Rep: Reputation: 899Reputation: 899Reputation: 899Reputation: 899Reputation: 899Reputation: 899Reputation: 899
So you want to block the ip's you got from
Quote:
netstat -atun | grep SYN_RECV | awk ‘{print $5}’ | cut -d: -f1 |sort | uniq -d | sort -n > blocked.ips
Try


Quote:
#!/bin/bash
IPT=/sbin/iptables
SPAMLIST=”spamlist”
$IPT -N $SPAMLIST
for i in `cat blocked.ips`
do
$IPT -A $SPAMLIST -s $i -j LOG
$IPT -A $SPAMLIST -s $i -j DROP
done
#$IPT -I INPUT -j $SPAMLIST
#$IPT -I OUTPUT -j $SPAMLIST
#$IPT -I FORWARD -j $SPAMLIST
Why do you use
Quote:
BADIPS=$(egrep -v -E “^#|^$” /root/iptables/blocked.ips)
The command gives errors, and I don't see the use for it.
 
Old 10-12-2009, 08:09 AM   #13
kotlt
LQ Newbie
 
Registered: Mar 2006
Posts: 11

Original Poster
Rep: Reputation: 0
Thank you repo, it work now. Can you please help me a question.

About above script, how to make old-block-ip won't be replaced ?

I mean if my current blocked.ips have two ip
1.1.1.1
and
1.2.2.2

And another ip must be block is 1.3.3.3 will be add in block.ips. Then, blocked.ips will have 3 ips.

My current script don't do that, it remove two old ips and replace by new ip.


Sorry my bad english.
 
Old 10-12-2009, 08:11 AM   #14
repo
LQ 5k Club
 
Registered: May 2001
Location: Belgium
Distribution: Arch
Posts: 8,529

Rep: Reputation: 899Reputation: 899Reputation: 899Reputation: 899Reputation: 899Reputation: 899Reputation: 899
Quote:
netstat -atun | grep SYN_RECV | awk ‘{print $5}’ | cut -d: -f1 |sort | uniq -d | sort -n > blocked.ips
use
Quote:
netstat -atun | grep SYN_RECV | awk ‘{print $5}’ | cut -d: -f1 |sort | uniq -d | sort -n >> blocked.ips
to add the ip's at the end of the file

Last edited by repo; 10-12-2009 at 08:14 AM.
 
  


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
setup crontab job user ust Linux - Newbie 2 07-07-2009 12:08 AM
Hourly Crontab Setup gbkyle Linux - Newbie 2 10-06-2004 08:54 PM
Setup Crontab for AWSTATS hct224 Linux - Newbie 0 02-28-2004 11:34 AM
Pleas help! henkas Linux - Networking 13 11-04-2003 06:34 AM
Crontab Help Pleas monkeymartin Slackware 1 04-24-2003 03:39 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:31 AM.

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