首页 文章

DD-WRT(pptp-client):连接到VPN时自动添加路由和DNS信息?

提问于
浏览
3

我正在使用DD-WRT的PPTP客户端连接到VPN . 在Services / PPTP Client配置页面上,我指定了远程子网192.168.112.0和掩码255.255.255.0 .

Build 连接后,将自动添加该路由 . 但是,还有其他通过该连接可用的子网,例如192.168.7.0,但我必须在命令行手动添加这些路由才能使其正常工作 .

我相信VPN服务器必须发送路由列表,因为当我使用Windows XP连接到VPN时,所有这些子网的路由会自动添加到路由表中 .

有没有办法让DD-WRT在 Build 连接时自动添加这些路由?也就是说,如果网络配置在VPN服务器后面发生变化,我就不必手动编辑DD-WRT上的路由表 .

DNS服务器也一样,有没有办法避免手动输入用于VPN连接的DNS服务器?

2 回答

  • 2

    当ppp连接启动此脚本时:

    /etc/ppp/ip-up
    

    在您的系统中执行 . 请注意,有一些变量从服务器传递 . 阅读最后的 for 语句,它将启动更多脚本:

    #!/bin/sh
    # This script is run by pppd after the link is established.
    # It executes all the scripts available in /etc/ppp/ip-up.d directory,
    # with the following parameters:
    # $1 = interface name (e.g. ppp0)
    # $2 = tty device
    # $3 = speed
    # $4 = local IP address
    # $5 = remote IP address
    # $6 = ipparam (user specified parameter, see man pppd)
    ifconfig $1 mtu 1280 || true
    
    cd /etc/ppp/ip-up.d || exit
    
    for SCRIPT in *.sh ; do
            . ./"${SCRIPT}" "$@"
    done
    

    /etc/ppp/ip-up.d 文件夹中,我有一个名为 40-dns.sh 的文件 . 它看起来像这样,它将使用VPN服务器发送的DNS服务器设置 /etc/resolve.conf

    #!/bin/sh    
    # Handle resolv.conf generation when usepeerdns pppd option is being used.
    # Used parameters and environment variables:
    # $1 - interface name (e.g. ppp0)
    # $USEPEERDNS - set if user specified usepeerdns
    # $DNS1 and $DNS2 - DNS servers reported by peer
    
    if [ "$USEPEERDNS" ]; then
    
            if [ -x /sbin/resolvconf ]; then
                    {
                            echo "# Generated by ppp for $1"
                            [ -n "$DNS1" ] && echo "nameserver $DNS1"
                            [ -n "$DNS2" ] && echo "nameserver $DNS2"
                    } | /sbin/resolvconf -a "$1"
            else
                    # add the server supplied DNS entries to /etc/resolv.conf
                    # (taken from debian's 0000usepeerdns)
    
                    # follow any symlink to find the real file
                    REALRESOLVCONF=$(readlink -f /etc/resolv.conf)
    
                    if [ "$REALRESOLVCONF" != "/etc/ppp/resolv.conf" ]; then
    
                            # merge the new nameservers with the other options from the old configuration
                            {
                                    grep --invert-match '^nameserver[[:space:]]' $REALRESOLVCONF
                                    cat /etc/ppp/resolv.conf
                            } > $REALRESOLVCONF.tmp
    
                            # backup the old configuration and install the new one
                            cp -dpP $REALRESOLVCONF $REALRESOLVCONF.pppd-backup
                            mv $REALRESOLVCONF.tmp $REALRESOLVCONF
    
                            # correct permissions
                            chmod 0644 /etc/resolv.conf
                            chown root:root /etc/resolv.conf
                    fi
            fi
    
    fi
    

    对于要在连接 Build 的路由表中推送的路由,您应该能够执行类似的操作 . 转到pppd手册页以查看您需要使用的变量名称 .

    这些代码示例来自我的Gentoo Linux PC,但这些东西是Linux通用的,所以它也适用于DD-WRT .

  • 1

    虽然以前的答案对于Linux来说是正确的,但您无法在某些ddwrt路由器上轻松编辑或添加文件 .

    我使用的所有4个ddwrt路由器在运行pptp客户端时生成这些文件,因此无法仅更改或添加文件 .

    这是一个似乎适用于大多数路由器的解决方法http://stadar.org/content/ddwrt-pptp-client-add-routes-after-connection

相关问题