Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I want to make iptables redirect internally from one port to another only if a service is listening to that port. Is it possible?

share|improve this question
add comment

2 Answers

AFAIK IPtables doesn't have enough information to do that effectively. You would probably need to write a script that uses netcat or data from netstat to decide if the port is listening, and if so do the redirect. Run that script from cron.

share|improve this answer
    
You think a pid or a lock file would help? Maybe using conntrack or state matching modules? –  omgirkal May 28 '11 at 21:29
    
Don't force yourself to use iptables. You can check with a if command from netstat, lsof, ... result and do the redirect. –  quanta May 29 '11 at 2:04
add comment

Sure, you can do it:

iptables -t nat -A PREROUTING -i ethx -p tcp --dport $srcPort -j REDIRECT --to-port $dstPort

share|improve this answer
    
That's what I already have, but that doesn't make sure a service is listening or that the port is on a certain state. –  omgirkal May 28 '11 at 19:24
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.