It would work if you specify the lan network interface in the iptables rule, and run the nc command from another machine on the lan.
But by default iptables doesn't filter loopback traffic, and I'm not sure how you would set it up (breaking loopback traffic will break lots of local services). If you need to enable this locally for testing puprposes then an easier solution would perhaps be to use something like socat:
Code:
yum install socat
socat TCP-LISTEN:AAAA,fork TCP:localhost:BBBB &
then run your tests, if it works locally and you finish testing then you can kill the socat process, and the iptables rules explained above will route the packets for incoming network connections.
To do it via iptables would require a filter table rule on the OUTPUT chain I assume, but I've not done this before so can't advise on that (The Firewall gui doesn't even have the loopback device lo in the interface list, so I assume it's not trivial or common)
---------- Post added at 04:16 PM CST ---------- Previous post was at 01:59 PM CST ----------
experimented this afternoon, and it's pretty straightforward actually, applying a rule to the OUTPUT chain in the nat table (not filter as I incorrectly suggested above)
Code:
iptables -t nat -A OUTPUT -p tcp --dport AAAA -j REDIRECT --to-ports BBBB
now (assuming something is listening on tcp port BBBB)
Code:
nc -v -w2 -z localhost AAAA
nc: connect to localhost port AAAA (tcp) failed: Connection refused
Connection to localhost AAAA port [tcp/*] succeeded!
the first Connection refused appears because the iptablesl only does ipv4 forwarding, and there are two localhost addresses defined in /etc/hosts, ::1 for ipv6 and 127.0.0.1 for ipv4 you can check with telnet:
Code:
$ telnet localhost AAAA
Trying ::1...
telnet: connect to address ::1: Connection refused
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
you won't get the refused message if you explicitly use 127.0.0.1
Code:
$ nc -v -w2 -z 127.0.0.1 AAAA
Connection to 127.0.0.1 AAAA port [tcp/*] succeeded!
(you need ip6tables for ipv6 rules)