Skip to content

Instantly share code, notes, and snippets.

@edubart
Last active June 6, 2017 19:17
Show Gist options
  • Save edubart/4081531 to your computer and use it in GitHub Desktop.
Save edubart/4081531 to your computer and use it in GitHub Desktop.
vpn through pppd
#!/bin/bash
interface=eth0
address=
gateway=
broadcast=
netmask=
vpn_local=10.0.0.1
vpn_remote=10.0.0.2
ssh_user=root
ssh_address=107.182.226.113
ssh_port=22
dnses=('8.8.8.8', '8.8.4.4')
ssh_opts="-c chacha20-poly1305@openssh.com -i /home/bart/.ssh/id_rsa"
pppd_opts="nodeflate nobsdcomp"
msg() {
cols=$(tput cols)
len=${#1}
let columns="cols - len - 6"
printf "\e[1;97m%s%*s" "$1" $columns
}
msgend() {
printf "\e[1;92m[DONE]\e[0m\n"
}
msgendfail() {
printf "\e[1;91m[FAIL]\e[0m\n"
exit 1
}
detect() {
msg "Reading /etc/conf.d/vpn configuration..."
#[ -e /etc/conf.d/vpn ] || msgendfail
#source /etc/conf.d/vpn || msgendfail
if [ -z "$ssh_user" ] || [ -z "$ssh_address" ] || [ -z "$ssh_port" ] || [ -z "$vpn_local" ] || [ -z "$vpn_remote" ]; then
msgendfail
fi
msgend
msg "Detecting network configuration..."
if [ -z "$gateway" ]; then
gateway=`ip route | grep -E "default via [0-9.]* dev $interface" | sed "s/.*via \([0-9].*\) dev $interface.*/\1/"`
if [ -z "$gateway" ]; then
gateway=`ip route | grep -E "$ssh_address via [0-9.]* dev $interface" | sed "s/.*via \([0-9].*\) dev $interface.*/\1/"`
fi
fi
if [ -z "$address" ]; then
address=`ip addr show $interface | grep "inet " | sed 's/.*inet \([0-9.]*\)\/.*/\1/'`
fi
if [ -z "$broadcast" ]; then
broadcast=`ip addr show $interface | grep "inet " | sed 's/.*brd \([0-9.]*\).*/\1/'`
fi
if [ -z "$netmask" ]; then
netmask=`ip addr show $interface | grep "inet " | sed 's/.*inet [0-9.]*\/\([0-9]*\).*/\1/'`
fi
if [ -z "$address" ] || [ -z "$gateway" ] || [ -z "$broadcast" ] || [ -z "$netmask" ]; then
msgendfail
fi
msgend
}
start() {
msg "Writing nameservers..."
echo -n > /etc/resolv.conf
for dns in ${dnses[*]}; do
echo "nameserver $dns" >> /etc/resolv.conf
done
msgend
msg "Killing dhcpcd and pppd daemons..."
killall -qw dhcpcd
killall -qw pppd
msgend
msg "Configuring network..."
ip addr flush dev $interface || msgendfail
ip link set dev $interface up || msgendfail
ip addr add $address/$netmask broadcast $broadcast dev $interface || msgendfail
ip route replace default via $gateway dev $interface || msgendfail
ip route replace $ssh_address via $gateway dev $interface || msgendfail
msgend
msg "Connecting to VPN through pppd..."
/usr/sbin/pppd \
updetach noauth $pppd_opts silent pty \
"/usr/bin/ssh $ssh_opts -p $ssh_port $ssh_user@$ssh_address sudo /usr/sbin/pppd nodetach notty noauth" \
ipparam vpn $vpn_local:$vpn_remote || msgendfail
gotvpnaddr=`ip addr show ppp0 | grep "inet " | sed 's/.*inet \([0-9.]*\)[\/ ].*/\1/'`
if [ ! "$gotvpnaddr" == "$vpn_local" ]; then
killall -q pppd
msgendfail
fi
msgend
msg "Changing default gateway..."
ip route replace default via $vpn_remote || msgendfail
msgend
}
stop() {
msg "Replacing old gateway..."
ip route replace default via $gateway dev $interface || msgendfail
msgend
msg "Waiting pppd to finish..."
killall -qw pppd
msgend
}
case "$1" in
start)
detect
start;;
stop)
detect
stop;;
info)
detect
echo interface $interface $address/$netmask broadcast $broadcast gw $gateway
echo vpn local $vpn_local remote $vpn_remote
echo dnses ${dnses[@]}
echo remote ssh $ssh_user@$ssh_address:$ssh_port
;;
restart)
detect
stop
sleep 1
start;;
*)
echo "usage: $0 {start|stop|restart|info}";;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment