Last active
January 8, 2022 16:34
-
-
Save klaxa/99b42f657661990398f5 to your computer and use it in GitHub Desktop.
Shell script to set up an ssh vpn tunnel and set default routes.
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/bash | |
if [[ $# < 2 ]] | |
then | |
INTERNAL=tun0 | |
EXTERNAL=eth0 | |
else | |
INTERNAL=$1 | |
EXTERNAL=$2 | |
fi | |
echo 1 > /proc/sys/net/ipv4/ip_forward | |
iptables -t nat -A POSTROUTING -o $EXTERNAL -j MASQUERADE | |
iptables -A FORWARD -i $EXTERNAL -o $EXTERNAL -m state --state RELATED,ESTABLISHED -j ACCEPT | |
iptables -A FORWARD -i $INTERNAL -o $EXTERNAL -j ACCEPT |
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/bash | |
sudo ssh -w0:0 $@ true & | |
#tmux new-session -d "sudo ssh -w0:0 $@ true" | |
sleep 3 | |
sudo ssh "$@" "ifconfig tun0 192.168.42.1 netmask 255.255.255.0" | |
sudo ifconfig tun0 192.168.42.2 netmask 255.255.255.0 | |
ping -c 1 192.168.42.1 2> /dev/null > /dev/null | |
ret=$? | |
if [[ "$ret" -ne "0" ]] | |
then | |
echo "Tunnel setup failed." | |
exit 1 | |
fi | |
gateway=$(route -n | grep 0.0.0.0 | head -n 1 | awk '{print $2}') | |
host=$(echo "$@" | awk '{ print $NF }') | |
sudo route add -net $host netmask 255.255.255.255 gw $gateway | |
sudo route del default | |
sudo route add default gw 192.168.42.1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment