最近搞了个pptp vpn,但每次都要手动去连接,手动去指定路由,着实麻烦,搜索了下,dnsmasq可以对某域名指定DNS,便利用它来做个开机自动处理.
1.安装dnsmasq
sudo apt-get install dnsmasq
2.配置dnsmasq
sudo vi /etc/dnsmasq.conf
添加vpn连接后获取网关和要指定的域名,我vpn获取的网关是10.0.77.1,添加如下域名:
server=/www.google.com/10.10.77.1
server=/www.facebook.com/10.10.77.1
server=/www.youtube.com/10.10.77.1
server=/google.com/10.10.77.1
server=/facebook.com/10.10.77.1
server=/youtube.com/10.10.77.1
server=/twitter.com/10.10.77.1
server=/www.google.com.hk/10.10.77.1
server=/google.com.hk/10.10.77.1
server=/www.youtube.it/10.10.77.1
server=/googlevideo.com/10.10.77.1
3.配置pptp vpn
pptpsetup -create vpnname -server xxx.xxx.xxx.xxx -username username -password passwd -encrypt
设置完成后在/etc/ppp/peers/有该配置文件
4.做vpn连接脚本,自动添加刚才指定的那些域名的路由(代码写的不好,仅供参考)
vi /home/pon_vpnname.py
#!/usr/bin/env python
#_*_coding:utf-8_*_
import os
import sys
import time
domain = '''www.google.com
www.facebook.com
www.youtube.com
facebook.com
google.com.hk
googlevideo.com
twitter.com'''
dnsserver = '10.10.77.1'
def add_route(site,dnsserver):
dnsinfo = os.popen('host %s %s' % (site,dnsserver)).readlines()
#通过host命令获取指定dns的域名解析ip
for line in dnsinfo:
if 'has address' in line:
siteip = line.split()[3]
spip = siteip.split('.')
#只获取该域名IP的前2位做网段
sitenet = spip[0] + '.' + spip[1] + '.0.0'
break
#添加静态路由
print 'route add -net %s netmask 255.255.0.0 gw %s' % (sitenet,dnsserver)
os.system('route add -net %s netmask 255.255.0.0 gw %s' % (sitenet,dnsserver))
os.system('pon vpnname')
time.sleep(5)
def main():
val = 1
while 1:
data = os.popen('ip ad | grep ppp').read()
if data == '':
os.system('pon vpnname')
time.sleep(5)
elif dnsserver in data:
break
else:
#某些特殊网络环境下无法连接时,先断开,只尝试3次
os.system('poff vpnname')
time.sleep(5)
val += 1
if val == 3:
print "VPN connection failed, please check vpn server"
sys.exit(2)
for site in domain.split('\n'):
try:
add_route(site,dnsserver)
except UnboundLocalError:
print 'ERROR:route add %s faild' % val
if __name__ == '__main__':
main()
5.将该脚本添加到开机启动,开机后自动连接.
sudo vi /etc/rc.local
python /home/pon_vpnname.py &
还有些工具也很好用,比如chrome的Proxy SwitchySharp,简单配置下即可使用。
ubuntu14.04自带的DNS服务器dnsmasq://m.ajphoenix.com/linux/14667.html
Ubuntu Server 12.04下Cobbler+DNSmasq+tftpd-hpa的安装配置://m.ajphoenix.com/linux/11884.html
Ubuntu部署Dnsmasq实现Kickstart自动安装操作系统://m.ajphoenix.com/linux/10765.html
Ubuntu 14.04下安装dnsmasq解决网速慢的问题://m.ajphoenix.com/linux/9808.html
Ubuntu服务器搭建VPN(PPTP)://m.ajphoenix.com/linux/15005.html