Skip to content

Instantly share code, notes, and snippets.

@selankon
Last active June 4, 2018 11:42
Show Gist options
  • Select an option

  • Save selankon/3e80819eff9447a594da6b49b033aed7 to your computer and use it in GitHub Desktop.

Select an option

Save selankon/3e80819eff9447a594da6b49b033aed7 to your computer and use it in GitHub Desktop.
Like VPN using ssh. Script that creates an ssh tunel associated to a "tun" interface to use it as default interface.
#!/bin/bash
# Like VPN using ssh. Script that creates an ssh tunel associated to a "tun" interface to use it as default interface. See man ssh option -w for more info.
# 1. Establish ssh tunel
# 2. Creates and associate it to the tun interface
# 3. Redirect al system trafic to this interface
#
# Like tipical VPN connections, all your traffic is tunneled by this p2p connection
# IMPORTANT:
# On the client in /etc/ssh/ssh_config set Tunnel point-to-point
# On the sercer in /etc/ssh/sshd_config set PermitTunnel point-to-point
VPN_GATEWAY=192.168.42.1
ORIGINAL_GATEWAY=`ip route show | grep ^default | cut -d ' ' -f 2-5`
DOMAIN="dyn.dns.domain"
INTERFACE=tun5
REMOTEADDRESS=$( dig +short $DOMAIN)
MYIP=192.168.42.2
echo "Server ip found: " $REMOTEADDRES
sudo ssh -o PermitLocalCommand=yes -o LocalCommand=\
"sudo ifconfig $INTERFACE $MYIP pointopoint $VPN_GATEWAY netmask 255.255.255.0;"\
"sudo ip route add $REMOTEADDRESS $ORIGINAL_GATEWAY;"\
"sudo ip route add $VPN_GATEWAY dev $INTERFACE;"\
"sudo ip route add 0.0.0.0/1 via $VPN_GATEWAY dev $INTERFACE;"\
"ip route add 128.0.0.0/1 via $VPN_GATEWAY dev $INTERFACE" -o ServerAliveInterval=60 -w 5:5 root@$REMOTEADDRESS "sudo ifconfig $INTERFACE $VPN_GATEWAY pointopoint $MYIP netmask 255.255.255.0; echo tun5 ready"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment