Skip to content

Instantly share code, notes, and snippets.

@rhybroy
Last active December 10, 2015 05:38
Show Gist options
  • Save rhybroy/4388843 to your computer and use it in GitHub Desktop.
Save rhybroy/4388843 to your computer and use it in GitHub Desktop.
generate vpn routes from ip txt
#
#Google
#
216.73.93.70/31
216.73.93.72/31
216.239.32.0/19
64.233.160.0/19
66.249.80.0/20
72.14.192.0/18
209.85.128.0/17
66.102.0.0/20
74.125.0.0/16
64.18.0.0/20
207.126.144.0/20
173.194.0.0/16
#
#Twitter
#
199.16.156.0/22
199.59.148.0/22
#
#Dropbox
#
199.47.216.0/22
#wiki.dropbox.com
174.36.51.42
#yfrog
208.94.0.61/32
208.94.0.21/32
208.94.0.38/32
#
#Sourceforge
#
12.31.165.64/27
208.48.95.16/28
216.34.181.0/24
#
#wordpress.com
#
76.74.255.0/24
76.74.254.0/24
74.200.244.0/24
74.200.243.0/24
72.233.69.6/32
72.233.2.58/32
72.233.119.192/26
72.233.111.161/32
72.233.104.107/32
72.232.146.117/32
216.151.210.124/32
208.95.156.128/27
#
#Facebook
#
69.63.179.25
69.63.178.128/25
69.63.184.0/25
66.220.144.128/25
66.220.155.0/24
69.171.232.128/25
66.220.157.0/25
69.171.244.0/24
#
#SugarSync
#
74.201.86.110/32
74.201.86.118/32
74.201.86.119/32
208.85.54.98/32
208.85.54.99/32
65.240.141.55/32
66.77.66.89/32
74.201.86.117/32
74.201.86.120/32
74.201.86.121/32
74.201.86.21/32
74.115.204.1/24
#
#SendGrid (path.com)
#
#sendgrid.net
208.115.214.0/24
74.63.202.0/24
75.126.200.128/27
75.126.253.0/24
67.228.50.32/27
174.36.80.208/28
174.36.92.96/27
69.162.98.0/24
74.63.194.0/24
74.63.234.0/24
74.63.235.0/24
#sendgrid.biz
208.115.235.0/24
74.63.231.0/24
74.63.247.0/24
74.63.236.0/24
208.115.239.0/24
173.193.132.0/24
173.193.133.0/24
208.117.48.0/20
50.31.32.0/19
#
#bit.ly
#
69.58.188.39/28
#
#cn.nytimes.com
#
61.244.110.205
#a1116.x.akamai.net
72.246.188.200/26
#
#who.is for test
#
174.36.202.234
#!/usr/bin/env python
#-*- coding:utf-8 -*-
__author__ = 'lucas'
import string
import sys
import os
import time
g_netshCommand = ''
g_routeDefFilePath = ''
g_outcontent = ''
def main():
global g_routeDefFilePath, g_outcontent
g_outcontent += '#!/bin/sh' + '\n'
g_outcontent += 'export PATH="/bin:/sbin:/usr/sbin:/usr/bin:/usr/local/bin"' + '\n'
g_outcontent += "OLDGW=`netstat -nr | grep '^default' | grep 'ppp' | sed 's/default *\([0-9\.]*\) .*/\1/'`" + '\n'
g_outcontent += 'if [ -z $OLDGW ];then' + '\n'
g_outcontent += ' exit 0' + '\n'
g_outcontent += 'fi' + '\n' + '\n'
g_outcontent += 'echo "${OLDGW}" > /tmp/pptp_oldgw' + '\n' + '\n'
g_outcontent += 'dscacheutil -flushcache' + '\n' + '\n'
#cmdnetsh = "netstat -nr | grep '^default' | grep -v 'ppp' | gsed 's/default *\([0-9\.]*\) .*/\1/'"
#os.system(cmdnetsh)
#gateway = raw_input('gateway index: ')
file = raw_input('route definiation file[default-ips]: ')
if not file:
file = 'default-ips'
fh = loadfile(file)
g_routeDefFilePath = os.path.dirname(file)
if fh is None:
sys.exit('File [' + file + '] not found')
fulltext = fh.readlines()
fh.close()
checkAddOrDelete()
processRoute(fulltext, 0)
fos = open('ip-up', 'w')
fos.write(g_outcontent)
fos.close()
def checkline(text, idx):
global g_routeDefFilePath
if text.startswith('#include '):
incFile = str(text).replace('#include', '').strip()
fullPath = os.path.join(g_routeDefFilePath, incFile)
fh = loadfile(fullPath)
fulltext = fh.readlines()
fh.close()
print 'process file', incFile
time.sleep(1)
processRoute(fulltext, idx)
return False
if text.startswith('#'):
return False
return True
def loadfile(file):
if os.path.exists(file):
return open(file, 'r')
return None
def processRoute(fulltext, idx):
global g_netshCommand, g_outcontent
for line in fulltext:
if checkline(line, idx):
param = {}
param['route'] = line.strip()
#param['idx'] = idx
cmd = g_netshCommand.safe_substitute(param)
print cmd
g_outcontent += cmd + '\n'
#os.system(cmd)
#print '%18s' % line.rstrip(), 'ok'
pass
def checkAddOrDelete():
global g_netshCommand
add = raw_input('Add or Delete a|d : ')
if add.lower() == 'a':
g_netshCommand = string.Template('route add $route "${OLDGW}"')
if add.lower() == 'd':
g_netshCommand = string.Template('route delete $route "${OLDGW}"')
if add.lower() != 'a' and add.lower() != 'd':
checkAddOrDelete()
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
sys.exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment