CHNrouters 添加 Via Python For Win

虽然有个批处理版本 但是实际使用中发现问题很多 比如不能正确识别PPPOE地址等。
自己用Python写了一个 和CHNrouters规则不完全兼容 需要转换有脚本
另外 这个是For Win的 linux用户根本不需要
[python]#!/usr/bin/env python
# -*- coding=utf-8 -*-
#Using GPL v2
#Author: [email protected]
#2011/4/25 03:11
"""
Usage:
Just A Route Adder of CHNROUTES for Windows that Don't Use OpenVPN
http://code.google.com/p/chnroutes/
rulers:
http://chnroutes.googlecode.com/files/routes.txt
"""

import win32ras
import socket
import subprocess
print 'Via http://ihipop.info With Python'
'''
import wmi
c = wmi.WMI()
for interface in c.Win32_NetworkAdapterConfiguration (IPEnabled=1):
print interface.Description, interface.MACAddress,interface.IPAddress[0]
'''

def disp(content):
print content.decode('UTF-8').encode('GBK')

if __name__ == '__main__':

blacklist='PPTP,L2TP,SSTP,IKEv2,VPN'
disp('正在检测是否有PPTP/L2TP/SSTP/IKEv2等VPN链接')

#PPTP等VPN会干扰地址检测 断开先 这里没做OpenVPN检查
active_connections = win32ras.EnumConnections()
for active_connection in active_connections:
#print active_connection[2]
if active_connection[2].lower() in blacklist.lower():
disp('发现'+active_connection[3]+'链接'+active_connection[1]+'正在断开中')
win32ras.HangUp(active_connection[0])

#本地地址
localIP = socket.gethostbyname(socket.gethostname())
result = subprocess.Popen('route print',stdout=subprocess.PIPE,shell=True)
result.wait()

gateway = False

lines = result.stdout.readlines()
for line in lines:
if localIP in line.rstrip():
gateways = line.split()
#print gateway
if '0.0.0.0' not in gateways:
print 'Skiping'
continue

gateway = gateways[2]
#print gateway
break

if not gateway:
disp('查找默认网关失败')
exit()

disp('选定默认网关: '+gateway)
disp('正在执行路由添加')
rulers=[]
try:
#读取外部的routes文件 可以用convertchn.py转换
routes = open('routes', 'rb')
lines = routes.readlines()
for line in lines:
rulers.append(line.rstrip())
#print rulers
routes.close()
except:
disp('外部规则读取失败 使用内部规则')
rulers.append('219.230.0.0/255.255.0.0')
rulers.append('211.65.0.0/255.255.0.0')
rulers.append('10.49.0.0/255.255.0.0')
rulers.append('172.0.0.0/255.0.0.0')
rulers.append('202.195.0.0/255.255.0.0')

s = ''
if len(rulers):
for ruler in rulers:
a,b = ruler.split('/')
if a and b:
exe = 'route add %s MASK %s %s' %(a,b,gateway)

result = subprocess.Popen(exe,stderr=subprocess.PIPE,stdout=subprocess.PIPE,shell=True)
num = result.wait()
content = result.stderr.readlines()

if content:
#Log content
if s:
s = s + '\n'
s = s + '%s/%s Add Failed' %(a,b)
s = s + '\n'
for c in content:
s = s + c.rstrip()
pass
#输出错误结果
if s:
print s
print 'Done!'

#调用rasphone拨号
disp('正在启动VPN链接')
data=subprocess.Popen('rasphone',stdout=subprocess.PIPE,shell=True).wait()[/python]

规则转换
[python]#!/usr/bin/env python
# -*- coding=utf-8 -*-
#Using GPL v2
#Author: [email protected]
#2011/4/25 03:11
"""
Usage:
Just A Route Adder of CHNROUTES for Windows that Don't Use OpenVPN
http://code.google.com/p/chnroutes/
rulers:
http://chnroutes.googlecode.com/files/routes.txt
"""

import sys

print 'Via http://ihipop.info With Python'

try:
fname = sys.argv[1]
print 'Opening %s' %fname
try:
routes = open(fname, 'rb')
except:
print 'Open %s Failed !' %fname
exit()
except:
print 'Must Specify a Valid File name !'
exit()

print 'converting......... %s' %fname
rulers = []
finals = []
lines = routes.readlines()
for line in lines:
rulers.append(line.rstrip())
routes.close()
for ruler in rulers:
ruler = ruler.split()
a = ruler[1]
b = ruler[2]
finals.append('%s/%s' %(a,b))
#print final

print 'writing.........'
i=0
try:
routes = open('routes', 'w')
for final in finals:
routes.write(final)
routes.write('\r\n')
i = i +1
routes.close()
except:
print 'Write File Failed!'

print 'Done With %s Rulers' %i
[/python]

Author Info :
  • From:CHNrouters 添加 Via Python For Win
  • URL:https://blog.ihipop.com/2011/04/2255.html
  • Please Reserve This Link,Thanks!
  • 发表回复

    您的电子邮箱地址不会被公开。 必填项已用 * 标注