Skip to content

Instantly share code, notes, and snippets.

@mrozo
Forked from pkorpine/iperf3_loopback.sh
Last active January 17, 2025 20:06
Show Gist options
  • Save mrozo/a250edd36217d78e6c8286c962ffe514 to your computer and use it in GitHub Desktop.
Save mrozo/a250edd36217d78e6c8286c962ffe514 to your computer and use it in GitHub Desktop.
Create external loopback between two network interfaces
#!/bin/sh
# Scripted from http://serverfault.com/questions/127636/force-local-ip-traffic-to-an-external-interface
DEV_0="ens6f0"
DEV_1="ens6f1"
MAC_0=`ifconfig $DEV_0 | tr -s '\t' ' ' | grep -oE 'ether[ ]+([0-9a-f]{2}:)+[0-9a-f]{2}' | cut -d ' ' -f2`
MAC_1=`ifconfig $DEV_1 | tr -s '\t' ' ' | grep -oE 'ether[ ]+([0-9a-f]{2}:)+[0-9a-f]{2}' | cut -d ' ' -f2'`
IP_0="192.168.100.1"
IP_1="192.168.101.1"
FAKE_0="192.168.102.1"
FAKE_1="192.168.103.1"
# Setup IP addresses
ifconfig ${DEV_0} ${IP_0}/24
ifconfig ${DEV_1} ${IP_1}/24
# nat source IP ${IP_0} -> ${IP_0} -> ${FAKE_0} when going to ${FAKE_1}
iptables -t nat -A POSTROUTING -s ${IP_0} -d ${FAKE_1} -j SNAT --to-source ${FAKE_0}
# nat inbound ${FAKE_0} -> ${IP_0}
iptables -t nat -A PREROUTING -d ${FAKE_0} -j DNAT --to-destination ${IP_0}
# nat source IP ${IP_1} -> ${FAKE_1} when going to ${FAKE_0}
iptables -t nat -A POSTROUTING -s ${IP_1} -d ${FAKE_0} -j SNAT --to-source ${FAKE_1}
# nat inbound ${FAKE_1} -> ${IP_1}
iptables -t nat -A PREROUTING -d ${FAKE_1} -j DNAT --to-destination ${IP_1}
ip route add ${FAKE_1} dev ${DEV_0}
arp -i ${DEV_0} -s ${FAKE_1} ${MAC_1}
ip route add ${FAKE_0} dev ${DEV_1}
arp -i ${DEV_1} -s ${FAKE_0} ${MAC_0}
echo "Server: iperf3 -B 192.168.101.1 -s"
echo "Client: iperf3 -B 192.168.100.1 -c 192.168.103.1 -t 60 -i 10"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment