Skip to content

Instantly share code, notes, and snippets.

@pkorpine
Created February 17, 2017 10:59
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save pkorpine/dda468d25416b7553cc589423c910f2e to your computer and use it in GitHub Desktop.
Save pkorpine/dda468d25416b7553cc589423c910f2e 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}|awk 'NR==1 {print $5}'`
MAC_1=`ifconfig ${DEV_1}|awk 'NR==1 {print $5}'`
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