Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Route generating scripts for both pptp and ovpn on mac/linux/win

  • Loading branch information...
commit 6e3c1101421374efaee29fea11a3d192c6827370 1 parent baad55a
@fivesheep authored
View
61 chnroutes_ovpn_linux
@@ -0,0 +1,61 @@
+#!/usr/bin/env python
+
+import re
+import urllib
+
+VPNUPBASE="""#!/bin/sh
+export PATH="/bin:/sbin:/usr/sbin:/usr/bin"
+
+OLDGW=`ip route show | grep '^default' | sed -e 's/default via \\([^ ]*\\).*/\\1/'`
+
+if [ $OLDGW == '' ]; then
+ exit 0
+fi
+
+if [ ! -e /tmp/openvpn_oldgw ]; then
+ echo $OLDGW > /tmp/openvpn_oldgw
+fi
+
+"""
+
+VPNDOWNBASE="""#!/bin/sh
+export PATH="/bin:/sbin:/usr/sbin:/usr/bin"
+
+OLDGW=`cat /tmp/openvpn_oldgw`
+
+"""
+
+url=r'http://ftp.apnic.net/apnic/dbase/data/country-ipv4.lst'
+
+handler=urllib.urlopen(url)
+
+upfile=open('vpnup','w')
+downfile=open('vpndown','w')
+
+upfile.write(VPNUPBASE)
+upfile.write('\n')
+
+downfile.write(VPNDOWNBASE)
+downfile.write('\n')
+
+for line in handler.readlines():
+ if line.find(': cn ') < 0: continue
+ r=line.split(':')[1]
+ r=r.strip()
+ ip,mask=r.split('/')
+ ip=ip.split('.')
+ while len(ip) < 4:
+ ip.append('0')
+
+ mask=int(mask)
+
+ bm='1'*mask+'0'*(32-mask)
+ mask="%d.%d.%d.%d"%(int(bm[0:8],2),int(bm[8:16],2),int(bm[16:24],2),int(bm[24:32],2))
+
+ upfile.write('route add -net %s netmask %s gw $OLDGW\n'%('.'.join(ip),mask))
+ downfile.write('route del -net %s netmask %s\n'%('.'.join(ip),mask))
+
+downfile.write('rm /tmp/openvpn_oldgw\n')
+
+upfile.close()
+downfile.close()
View
66 chnroutes_ovpn_mac
@@ -0,0 +1,66 @@
+#!/usr/bin/env python
+
+import re
+import urllib2
+
+agent={'User-agent': 'Mozilla/6.0 (compatible; MSIE 6.0; Windows NT 5.2)'}
+
+
+
+VPNUPBASE="""#!/bin/sh
+export PATH="/bin:/sbin:/usr/sbin:/usr/bin"
+
+OLDGW=$( (scutil | grep Router | sed -e 's/.*Router : //')<< EOF
+open
+get State:/Network/Global/IPv4
+d.show
+quit
+EOF
+)
+
+if [ ! -e /tmp/openvpn_oldgw ]; then
+ echo "${OLDGW}" > /tmp/openvpn_oldgw
+fi
+
+dscacheutil -flushcache
+
+"""
+
+VPNDOWNBASE="""#!/bin/sh
+export PATH="/bin:/sbin:/usr/sbin:/usr/bin"
+
+if [ ! -e /tmp/openvpn_oldgw ]; then
+ exit 0
+fi
+
+ODLGW=`cat /tmp/openvpn_oldgw`
+
+"""
+
+url=r'http://ftp.apnic.net/apnic/dbase/data/country-ipv4.lst'
+req=urllib2.Request(url,None,agent)
+
+handler=urllib2.urlopen(req)
+
+upfile=open('vpnup','w')
+downfile=open('vpndown','w')
+
+upfile.write(VPNUPBASE)
+upfile.write('\n')
+downfile.write(VPNDOWNBASE)
+downfile.write('\n')
+
+for line in handler.readlines():
+ if line.find(': cn ') < 0: continue
+ r=line.split(':')[1]
+ r=r.strip()
+ ip,mask=r.split('/')
+ ip=ip.split('.')
+ if len(ip) < 4:
+ ip.append('0')
+ upfile.write('route add %s/%s "${OLDGW}"\n'%('.'.join(ip),mask))
+ downfile.write('route delete %s/%s ${OLDGW}\n'%('.'.join(ip),mask))
+
+downfile.write('\n\nrm /tmp/openvpn_oldgw\n')
+upfile.close()
+downfile.close()
View
36 chnroutes_ovpn_win
@@ -0,0 +1,36 @@
+#!/usr/bin/env python
+
+import re
+import urllib
+
+VPNUPBASE="""@echo off
+for /F "tokens=3 delims=: " %%a in ('route print ^| find "Default Gateway:"') do set gw=%%a
+
+"""
+
+url=r'http://ftp.apnic.net/apnic/dbase/data/country-ipv4.lst'
+
+handler=urllib.urlopen(url)
+
+upfile=open('vpnup.bat','w')
+
+upfile.write(VPNUPBASE)
+upfile.write('\n')
+
+for line in handler.readlines():
+ if line.find(': cn ') < 0: continue
+ r=line.split(':')[1]
+ r=r.strip()
+ ip,mask=r.split('/')
+ ip=ip.split('.')
+ while len(ip) < 4:
+ ip.append('0')
+
+ mask=int(mask)
+
+ bm='1'*mask+'0'*(32-mask)
+ mask="%d.%d.%d.%d"%(int(bm[0:8],2),int(bm[8:16],2),int(bm[16:24],2),int(bm[24:32],2))
+
+ upfile.write('route add %s mask %s %s metric 5\n'%('.'.join(ip),mask,'%gw%'))
+
+upfile.close()
View
61 chnroutes_pptp_mac
@@ -0,0 +1,61 @@
+#!/usr/bin/env python
+
+import re
+import urllib2
+
+agent={'User-agent': 'Mozilla/6.0 (compatible; MSIE 6.0; Windows NT 5.2)'}
+
+VPNUPBASE="""#!/bin/sh
+export PATH="/bin:/sbin:/usr/sbin:/usr/bin"
+
+OLDGW=`netstat -nr | grep '^default' | grep -v 'ppp' | sed 's/default *\\([0-9\.]*\\) .*/\\1/'`
+
+
+if [ ! -e /tmp/pptp_oldgw]; then
+ echo "${OLDGW}" > /tmp/pptp_oldgw
+fi
+
+dscacheutil -flushcache
+
+"""
+
+VPNDOWNBASE="""#!/bin/sh
+export PATH="/bin:/sbin:/usr/sbin:/usr/bin"
+
+
+if [ ! -e /tmp/pptp_oldgw]; then
+ exit 0
+fi
+
+ODLGW=`cat /tmp/pptp_oldgw`
+
+"""
+
+url=r'http://ftp.apnic.net/apnic/dbase/data/country-ipv4.lst'
+req=urllib2.Request(url,None,agent)
+
+handler=urllib2.urlopen(req)
+
+upfile=open('ip-up','w')
+downfile=open('ip-down','w')
+
+upfile.write(VPNUPBASE)
+upfile.write('\n')
+downfile.write(VPNDOWNBASE)
+downfile.write('\n')
+
+for line in handler.readlines():
+ if line.find(': cn ') < 0: continue
+ r=line.split(':')[1]
+ r=r.strip()
+ ip,mask=r.split('/')
+ ip=ip.split('.')
+ if len(ip) < 4:
+ ip.append('0')
+ upfile.write('route add %s/%s "${OLDGW}"\n'%('.'.join(ip),mask))
+ downfile.write('route delete %s/%s ${OLDGW}\n'%('.'.join(ip),mask))
+
+downfile.write('\n\nrm /tmp/pptp_oldgw\n')
+upfile.close()
+downfile.close()
+print "copy ip-up and ip-down to /etc/ppp/ and don't forget to make them executable."
Please sign in to comment.
Something went wrong with that request. Please try again.