Skip to content

Instantly share code, notes, and snippets.

@tasoseng
Created March 27, 2021 22:09
Show Gist options
  • Save tasoseng/09cd20da59f0a97efda5f7418897acd3 to your computer and use it in GitHub Desktop.
Save tasoseng/09cd20da59f0a97efda5f7418897acd3 to your computer and use it in GitHub Desktop.
port forward, nat
#!/bin/sh
# RUN THIS ON $ME
echo 1 > /proc/sys/net/ipv4/ip_forward
iptables -F
iptables -t nat -F
iptables -X
ME=192.168.1.123 # local ip to "impersonate" as
MY_PORT=1111
TARGET=172.16.1.1
TARGET_PORT=4444
iptables -t nat -A PREROUTING -p tcp --dport ${MY_PORT} -j DNAT --to-destination ${TARGET}:${TARGET_PORT}
iptables -t nat -A POSTROUTING -p tcp -d ${TARGET} --dport ${TARGET_PORT} -j SNAT --to-source ${ME}
# test:
# on $TARGET: nc -lk 4444
# on 3rd host (other than ME/TARGET): nc $ME 1111
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment