Skip to content

Instantly share code, notes, and snippets.

@wvengen
Last active April 28, 2019 17:24
Show Gist options
  • Save wvengen/4d6977afbb349ed369ddafdf02cdcc60 to your computer and use it in GitHub Desktop.
Save wvengen/4d6977afbb349ed369ddafdf02cdcc60 to your computer and use it in GitHub Desktop.
Update on-demand PPTP configuration for free VPNs
#!/bin/sh
#
# Update freevpn.se pptp configuration
#
# Since the password changes now and then, it's useful to update it automatically.
# Make sure you've installed lynx. Tested on Debian.
#
URL=https://www.freevpn.se/accounts/
PEERNAME=freevpn.se
# Gather data
TMP=`mktemp -t pptpupdate.XXXXXXXXXX`
lynx -dump "$URL" >"$TMP"
HOST=`cat "$TMP" | sed 's/^.*IP:\s*\([\.0-9]\+\).*$/\1/p;d' | head -n 1`
USER=`cat "$TMP" | sed 's/^.*Username:\s*\(\w\+\).*$/\1/p;d' | head -n 1`
PASS=`cat "$TMP" | sed 's/^.*Password:\s*\(\S\+\).*$/\1/p;d' | head -n 1`
[ "$HOST" -a "$USER" -a "$PASS" ] || exit 1
# Regenerate peer config
cat >"/etc/ppp/peers/$PEERNAME" <<-EOF
remotename $PEERNAME
linkname $PEERNAME
ipparam $PEERNAME
pty "pptp '$HOST' --nolaunchpppd "
name "$USER"
require-mppe
file /etc/ppp/options.pptp
EOF
# Update password
umask 077
grep -v '\b'"$PEERNAME"'\b' </etc/ppp/chap-secrets >/etc/ppp/chap-secrets.new &&
echo "$USER $PEERNAME $PASS" >>/etc/ppp/chap-secrets.new &&
mv /etc/ppp/chap-secrets.new /etc/ppp/chap-secrets
# Cleanup
rm -f "$TMP"
# Make sure you've installed lynx. Tested on Debian.
#
# On-demand dial configuration, expects routing script in /etc/ppp/010route.
#
# If run with the "start" argument, the vpn will be started right away (on demand).
#
# Based on: https://gist.github.com/wvengen/4d6977afbb349ed369ddafdf02cdcc60
#
URL=http://freevpnaccess.com/
PEERNAME=freevpnaccess.com
COUNTRY=UK
# Gather data
TMP=`mktemp -t pptpupdate.XXXXXXXXXX`
lynx -dump "$URL" | grep '\b'"$COUNTRY"'\b.*\bPPTP\b' >"$TMP"
HOST=`cat "$TMP" | sed 's/^.*\s\([0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+\)\s.*$/\1/p;d' | head -n 1`
PASS=`cat "$TMP" | sed 's/^.*\s\([0-9][0-9][0-9][0-9]\)\s*$/\1/p;d' | head -n 1`
USER=freevpnaccess.com
[ "$HOST" -a "$USER" -a "$PASS" ] || exit 1
# Regenerate peer config
cat >"/etc/ppp/peers/$PEERNAME" <<-EOF
remotename $PEERNAME
linkname $PEERNAME
ipparam $PEERNAME
pty "pptp '$HOST' --nolaunchpppd "
name "$USER"
require-mppe
persist
demand
idle 300
holdoff 10
file /etc/ppp/options.pptp
EOF
# Update password
umask 077
grep -v '\b'"$PEERNAME"'\b' </etc/ppp/chap-secrets >/etc/ppp/chap-secrets.new &&
echo "$USER $PEERNAME $PASS" >>/etc/ppp/chap-secrets.new &&
mv /etc/ppp/chap-secrets.new /etc/ppp/chap-secrets
# Cleanup
rm -f "$TMP"
if [ "$1" = "start" ]; then
pon "$PEERNAME" && \
sleep 2 && \
/etc/ppp/ip-up.d/010route
fi
@nottux
Copy link

nottux commented Apr 28, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment