LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


Reply
  Search this Thread
Old 12-20-2009, 10:44 AM   #1
Legolas891
LQ Newbie
 
Registered: Apr 2009
Posts: 22

Rep: Reputation: 15
Iptables redirect - No duplicate rules


Hi all,

I have a linux server and a script is verifying the database every day and adds an redirect rule in iptables for every client. The problem is that if a client is already redirected, it still adds the rule, because iptables is allowing that, and when i want to take out the redirect, i have to give the comand for every time it was added.

How i see it, there's some ways in bypassing this:
1. add another boolean field to the db, and verify if the client already has redirect. ( i want to avoid this )
2. before the script is trying to add the rule, delete it first, so it can be added again and again and in any case there will be only one redirect rule/client.

Is there any other way in doing this?

P.S. Sorry if i have a bad english
 
Old 12-20-2009, 11:28 AM   #2
tuxg
LQ Newbie
 
Registered: Dec 2009
Location: Vijayawada
Distribution: Red Hat, Solaris and so on...
Posts: 7

Rep: Reputation: 0
Hi,

What do you mean by a redirect?
Pls explain with the help of an example rule as to what you are trying to do so that someone here can help.

Cheers,
tuxg
 
Old 12-20-2009, 11:39 AM   #3
Legolas891
LQ Newbie
 
Registered: Apr 2009
Posts: 22

Original Poster
Rep: Reputation: 15
Gladly.

The command executed by the php script, that runs every day is
echo exec("/usr/bin/sudo /sbin/iptables -t nat -A PREROUTING -s $ip -p tcp --dport 80 -j DNAT --to-destination 192.168.0.1:81");
for every client. It redirects every client, every day to a page on my server, that also executes the command for deleting this rule after they've seen it. My goal is to display a message page to every client, once per day/week/month/when i want, and then they can click the button "Continue to requested page".
 
Old 12-20-2009, 11:44 AM   #4
nimnull22
Senior Member
 
Registered: Jul 2009
Distribution: OpenSuse 11.1, Fedora 14, Ubuntu 12.04/12.10, FreeBSD 9.0
Posts: 1,571

Rep: Reputation: 92
The only way I see, is to keep somewhere copy of "iptable-save" and check for duplicated rules.
Or to check the script it self.
 
Old 12-20-2009, 03:31 PM   #5
Legolas891
LQ Newbie
 
Registered: Apr 2009
Posts: 22

Original Poster
Rep: Reputation: 15
Any ideas, anyone...
 
Old 12-20-2009, 03:36 PM   #6
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
I'm with numnull on this -- what I do is, once the iptables script is ready to apply, I run it through `uniq` to get rid of duplicates. Later, if I am appending a rule let's say (I'll give this advice with your scenario in context) I would dump iptables-save to a file, and either grep/awk the file to see if the new rule already exists, OR you could add the new rule to the iptables-save script, run it through `uniq` and re-institute the whole script with iptables-restore.

Sasha
 
Old 12-20-2009, 03:44 PM   #7
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
PS - I should have added that, to make sure the script gets back into its proper order, I use cat -n to number the lines first, then uniq it ignoring the first field (the line number), then re-sort it back to its numerical line number, then use cut to remove the line numbers, and THEN it's ready to use again

Sasha
 
Old 12-20-2009, 03:47 PM   #8
nimnull22
Senior Member
 
Registered: Jul 2009
Distribution: OpenSuse 11.1, Fedora 14, Ubuntu 12.04/12.10, FreeBSD 9.0
Posts: 1,571

Rep: Reputation: 92
Looks like you want to make some kind dynamic filter of incoming connections.

Have you tried to find any other ways except of "iptables". May be some proxy can do it much better.
Iptables can do it, but may be there are many other better and appropriate for this purpose solutions?
 
Old 12-20-2009, 04:33 PM   #9
Legolas891
LQ Newbie
 
Registered: Apr 2009
Posts: 22

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by nimnull22 View Post
Looks like you want to make some kind dynamic filter of incoming connections.

Have you tried to find any other ways except of "iptables". May be some proxy can do it much better.
Iptables can do it, but may be there are many other better and appropriate for this purpose solutions?
I am not trying to make a filter. All i want is to redirect my clients, once a day(or week,or month, etc) to a page that displays a message.
It just redirects all the client's http traffic to my apache virtual server on port 81. ie - if client wants to access www.google.com they will be redirected to my page, wich ,on enter, executes the command for deleting the iptables redirect rule . There is a button "Continue to requested page" wich takes him to www.google.com. That's my idea. It's a "Message of the day" sistem, if you want.
 
Old 12-25-2009, 10:37 AM   #10
tuxg
LQ Newbie
 
Registered: Dec 2009
Location: Vijayawada
Distribution: Red Hat, Solaris and so on...
Posts: 7

Rep: Reputation: 0
Hi,

If it is something like motd, that means all the users will be redirected to the same webpage i suppose. In that case, why do you want to use the individual IP of each visitor? I mean, why can't you just use a blanket redirect such as the same rule without using "-s $ip" part? Would that serve your purpose?

Secondly, to add that rule only if it doesn't exist, you can probably call a custom chain as the last rule in your PREROUTING chain, flush the custom chain, and add the rule(s) you want every time.

HTH,
tuxg
 
  


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
iptables to redirect ip genderbender Linux - Networking 1 04-06-2008 01:53 AM
iptables 1.27a still loading rules after installing iptables 1.3.0 yawe_frek Linux - Software 1 06-07-2007 09:50 PM
iptables redirect patvrs Linux - Networking 15 08-02-2005 08:31 AM
IPTABLES - rules in /etc/sysconfig/iptables The_JinJ Linux - Newbie 6 11-20-2004 01:40 AM
iptables redirect bhartnett Linux - Networking 1 09-26-2001 09:11 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

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