ubuntu vpn

- apt-get install pptpd
- vim /etc/pptpd.conf 找到文件中呗注释掉的localip和remoteip,localip是vpn服务器ip,remoteip为登陆后 可分配的地址池,格式:
localip 192.168.1.1
remoteip 192.168.1.2-254

- vim /etc/ppp/pptpd-options
- 找到被注释掉ms-dns,修改成为dns服务器地址,格式:
ms-dns 172.16.26.253

- vim /etc/ppp/chap-secrets
- 加入用户账户,格式:
用户名 【tab】主机名【tab】 密码 【tab】 可分配ip
lyc 【tab】 * 【tab】Aa123456 【tab】 *


- vim /etc/sysctl.conf
- 去掉net.ipv4.ip_forward=1的注释。

- reboot

 

python代码

def add_user(name, password):
    np = "%s\t*\t%s\t*" % (name, password)
    with open(/etc/ppp/chap-secrets, r) as file:
        if not file.read().endswith(\n):
            np = \n + np
    with open(/etc/ppp/chap-secrets, a) as file:
        file.write(np)


def delete_user(name):
    results = []
    with open(/etc/ppp/chap-secrets, r) as file:
        lines = file.readlines()
    for line in lines:
        if not line.startswith(name):
            results.append(line)
    with open(/etc/ppp/chap-secrets, w+)   as file:
        file.writelines(results)


def change_password(name, password):
    with open(/etc/ppp/chap-secrets, r) as file:
        lines = file.readlines()
    for index, line in enumerate(lines):
        if not line.isspace():
            if line.startswith(name):
                lines[index] = "%s\t*\t%s\t*\n" % (name, password)
    with open(/etc/ppp/chap-secrets, w) as file:
        file.writelines(lines)


def print_user_file():
    with open(/etc/ppp/chap-secrets, r) as file:
        print file.read()


def get_user_status():
    result = []
    with open(/etc/ppp/chap-secrets, r+) as f:
        for line in f.readlines():
            if line:
                if line[0] != #:
                    lists = line.split()
                    if len(lists):
                        result.append({client: lists[0], secret: lists[2]})
    return result


def print_ip_file():
    with open(/etc/pptpd.conf, r) as filehandler:
        print filehandler.read()


def get_ip_status():
    result = {}
    with open(/etc/pptpd.conf, r) as file:
        lines = file.readlines()
    for line in lines:
        if line.startswith(localip):
            result[localip] = line.split()[1]
        elif line.startswith(remoteip):
            result[remoteip] = line.split()[1]
    return result


def change_ip(local_ip=None, remote_ip=None):
    local_changed = remote_changed = True
    if local_ip:
        local_changed = False
        local_ip = localip\t%s\n % local_ip
    if remote_ip:
        remote_changed = False
        remote_ip = remoteip\t%s\n % remote_ip
    with open(/etc/pptpd.conf, r) as file:
        lines = file.readlines()
    for index, line in enumerate(lines):
        if not line.isspace():
            if line[0] != #:
                if not local_changed:
                    if line.startswith(localip):
                        lines[index] = local_ip
                        local_changed = True
                if not remote_changed:
                    if line.startswith(remoteip):
                        lines[index] = remote_ip
                        remote_changed = True
    if not local_changed:
        if lines[-1][-1] != \n:
            local_ip = \n + local_ip
        lines.append(local_ip)
    if not remote_changed:
        if lines[-1][-1] != \n:
            remote_ip = \n + remote_ip
        lines.append(remote_ip)
    with open(/etc/pptpd.conf, w) as file:
        file.writelines(lines)

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。