Search code examples
linuxnetwork-programmingiptablesnetcat

using netcat for external loop-back test between two ports


I am writing a test script to exercise processor boards for a burn-in cycle during manufacturing. I would like to use netcat to transfer files from one process, out one Ethernet port and back into another Ethernet port to a receiving process. It looks like netcat would be an easy tool to use for this.

The problem is that if I set up the ethernet ports with IP addresses on separate IP sub nets and attempt to transfer data from one to the other, the kernel's protocol stack detects an internal route and although the data transfer completes as expected, it does NOT go out over the wire. The packets are routed internally.

That's great for network optimization but it foils the test I want to do.

Is there easy way to make this work? Is there a trick with iptables that would work? Or maybe things you can do to the route table?


Solution

  • Internal routing is preferred because in the default routing behaviour you have all the internal routes marked as scope link in the local table. Check this out with:

    ip rule show
    ip route show table local
    

    If your kernel supports multiple routing tables you can simply alter the local table to achieve your goal. You don't need iptables.

    Let's say 192.168.1.1 is your target ip address and eth0 is the interface where you want to send your packets out to the wire.

    ip route add 192.168.1.1/32 dev eth0 table local