Last active
August 29, 2015 14:04
-
-
Save JorgenEvens/3e58666f50bd521e27dc to your computer and use it in GitHub Desktop.
Use OpenWRT to tunnel traffic to remote site
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/sh | |
| REMOTE="tunnel-host -p 9099" | |
| REMOTE_NET="192.168.1.0/24" | |
| TUN=5 | |
| IF="tun$TUN" | |
| SSH_OPT="-c arcfour256 -o Protocol=2 -o ServerAliveCountMax=2 -o ServerAliveInterval=5 -o ConnectTimeout=5" | |
| echo $SSH_OPT | |
| sleep 15 # Wait for device bootup | |
| while [ 1 ]; do | |
| echo "Closing old connections" | |
| ssh $REMOTE $SSH_OPT -C "killall -9 sleep &" | |
| sleep 2 # Wait for old tunnels to close | |
| echo "Opening tunnel" | |
| ssh -w5:5 $REMOTE $SSH_OPT -C "ifconfig tun5 10.0.1.1 netmask 255.255.255.252 && sleep 999999999" & | |
| sleep 15 # Wait for tunnel to come up | |
| echo "Configure tunnel" | |
| ifconfig $IF 10.0.1.2 netmask 255.255.255.252 | |
| route add -net $REMOTE_NET gw 10.0.1.1 | |
| iptables -t nat -D POSTROUTING -o $IF -j MASQUERADE | |
| iptables -t nat -A POSTROUTING -o $IF -j MASQUERADE | |
| iptables -I FORWARD 1 -o $IF -j ACCEPT | |
| iptables -I FORWARD 1 -i $IF -j ACCEPT | |
| echo "Connection is up" | |
| # Wait until connection fails | |
| wait | |
| echo "Connection collapsed" | |
| killall ssh | |
| done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment